将if-else Logic添加到我的Applescript(取决于FIND是否匹配)

时间:2016-12-20 17:20:20

标签: applescript

我有一个执行大量文本操作的AppleScript,然后搜索文本字符串,选择该文本字符串,在选择上运行python脚本,插入输出,以及选择和复制输出。然后,AppleScript会采取更多行动。

问题:该Python脚本搜索并操作的文本字符串并不总是存在。如果没有,我希望它跳过python部分并恢复我的其余部分。

以下是该脚本的重要部分。在Python脚本切入之前,我将从AppleScript的最终操作开始:

replace "Claimed" using "" searching in text 1 of text document 1 options {starting at top:true, case sensitive:true}

#here is where my script searches for the text string  the Python script will operate on (and which may NOT be present):

find "^Mon .+" searching in text 1 of text document 1 options {search mode:grep, wrap around:true, case sensitive:true} with selecting match

#here's the python part (which shouldn't be triggered if the string, above, isn't found):

set unprocessed to contents of selection

set processed to (do shell script ("python ~/Library/Scripts/yelpmanip.py '" & unprocessed & "'"))

set contents of selection to processed

copy selection

#If python script didn't run, I'd want to resume below:

find "events" searching in text 1 of text document 1 options {search mode:grep, wrap around:true} with selecting match

tell application "System Events" to key code 126 using {shift down, command down}
#etc etc
end tell

1 个答案:

答案 0 :(得分:0)

您只需使用set match to find ...来获取结果,然后使用语法if found of match进行检查。

来源:https://nathangrigg.com/2012/06/bbedit-search-with-applescript

代码(未经测试):

replace "Claimed" using "" searching in text 1 of text document 1 options {starting at top:true, case sensitive:true}

#here is where my script searches for the text string  the Python script will operate on (and which may NOT be present):

set match to find "^Mon .+" searching in text 1 of text document 1 options {search mode:grep, wrap around:true, case sensitive:true} with selecting match

#here's the python part (which shouldn't be triggered if the string, above, isn't found):

if found of match then

    set unprocessed to contents of selection

    set processed to (do shell script ("python ~/Library/Scripts/yelpmanip.py '" & unprocessed & "'"))

    set contents of selection to processed

    copy selection

end if

#If python script didn't run, I'd want to resume below:

find "events" searching in text 1 of text document 1 options {search mode:grep, wrap around:true} with selecting match

tell application "System Events" to key code 126 using {shift down, command down}
#etc etc
end tell