使用 AppleScript 从剪贴板中提取特定行
Extract specific row from clipboard with AppleScript
我正在使用以下代码将剪贴板的内容放入我的 Automator 代码中:
set theString to get the clipboard
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
但是,如果用户复制了以下行:
word1;word2;word3
word4;word5;word6
如何只提取第二行?我对第一行不感兴趣,列数可以改变。
试试这个
set the clipboard to "word1;word2;word3
word4;word5;word6"
set theClipboard to (the clipboard)
set secondLine to (paragraph 2 of theClipboard)
set theString to secondLine
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
或
set the clipboard to "word1;word2;word3
word4;word5;word6"
set secondLine to paragraph 2 of (the clipboard)
set theString to secondLine
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
或
set the clipboard to "word1;word2;word3
word4;word5;word6"
set theString to paragraph 2 of (the clipboard)
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
以下示例 AppleScript 代码 对我有用:
set myTextItemDelimiter to ";"
if (clipboard info for string) is {} then
display dialog ¬
"The clipboard does not contain text." buttons {"OK"} ¬
default button 1 giving up after 5
return
end if
set theString to (the clipboard) as string
if theString does not contain myTextItemDelimiter then
display dialog ¬
"The clipboard does not contain the \"" & ¬
myTextItemDelimiter & "\" delimiter." buttons {"OK"} ¬
default button 1 giving up after 5
return
end if
if (count paragraphs of theString) is greater than 1 then
set theString to paragraph 2 of theString
else
display dialog ¬
"The clipboard only contains one line of text." buttons {"OK"} ¬
default button 1 giving up after 5
return
end if
set {TID, text item delimiters} to {text item delimiters, myTextItemDelimiter}
set theList to text items of theString
set text item delimiters to TID
return theList
备注:
我在代码中添加了一些错误处理。
我正在使用以下代码将剪贴板的内容放入我的 Automator 代码中:
set theString to get the clipboard
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
但是,如果用户复制了以下行:
word1;word2;word3
word4;word5;word6
如何只提取第二行?我对第一行不感兴趣,列数可以改变。
试试这个
set the clipboard to "word1;word2;word3
word4;word5;word6"
set theClipboard to (the clipboard)
set secondLine to (paragraph 2 of theClipboard)
set theString to secondLine
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
或
set the clipboard to "word1;word2;word3
word4;word5;word6"
set secondLine to paragraph 2 of (the clipboard)
set theString to secondLine
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
或
set the clipboard to "word1;word2;word3
word4;word5;word6"
set theString to paragraph 2 of (the clipboard)
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
以下示例 AppleScript 代码 对我有用:
set myTextItemDelimiter to ";"
if (clipboard info for string) is {} then
display dialog ¬
"The clipboard does not contain text." buttons {"OK"} ¬
default button 1 giving up after 5
return
end if
set theString to (the clipboard) as string
if theString does not contain myTextItemDelimiter then
display dialog ¬
"The clipboard does not contain the \"" & ¬
myTextItemDelimiter & "\" delimiter." buttons {"OK"} ¬
default button 1 giving up after 5
return
end if
if (count paragraphs of theString) is greater than 1 then
set theString to paragraph 2 of theString
else
display dialog ¬
"The clipboard only contains one line of text." buttons {"OK"} ¬
default button 1 giving up after 5
return
end if
set {TID, text item delimiters} to {text item delimiters, myTextItemDelimiter}
set theList to text items of theString
set text item delimiters to TID
return theList
备注:
我在代码中添加了一些错误处理。