我有数百个冗长的applescripts要编辑,我需要在每个脚本的不同位置找到并替换以下代码片段。
tell application "Adobe Photoshop CC 2015.5"
set myLayer to current layer of current document
if last character of mySport is "s" then
set contents of text object of myLayer to mySport & ""
else
set contents of text object of myLayer to mySport & "'s"
end if
end tell我想把它替换为
tell application "Adobe Photoshop CC 2015.5"
set myLayer to current layer of current document
set contents of text object of myLayer to mySport & "'s"
end tell有没有办法编写一个applescript来查找和替换几行代码?
code screen grab
第二个问题是如何处理引号中包含的撇号?
你可能知道我是一个艺术家,而不是一个开发人员或脚本编写者!前段时间我试图得到一个答案,但没有成功,现在这个问题变得非常关键。
非常感谢,期待着答复。
发布于 2016-08-14 04:05:25
最好是将此子例程设置为单独的脚本库,并在每个脚本中调用它。这样做,只有一个更改就足够了。我建议你下次这样做。
我努力寻找一种在脚本中进行更改的方法,但这并不容易。脚本编辑器的脚本编写能力非常有限。解决办法是使用GUI脚本,这意味着Apple在未来版本中所做的任何更改都可能不再起作用。
以下脚本模拟您的键盘操作,以搜索和替换NewString的CurString:
set myScript to "Users:imac27:Desktop:Testscript.scpt" -- path to your script
set CurString to "Set B to 2"
set NewString to "Set X to 5"
tell application "Script Editor"
open myScript
activate myScript
delay 2
tell application "System Events"
keystroke "f" using {option down, command down} --mode search & replace
keystroke tab using {shift down} -- got to search area
keystroke CurString -- set the search target
keystroke tab -- goto replace area
keystroke NewString -- set replace value
-- click on menu "Replace all " which is the 7th item of "Search" menu item (=item 14th of menu "Edit")
tell process "Script Editor" to click menu item 7 of menu of menu item 14 of menu 4 of menu bar 1
end tell
compile front document
save front document
close front document
end tell这个脚本打开脚本,执行搜索,替换,单击“替换”菜单,然后编译新版本,保存并关闭它。如果您有许多脚本,则必须为每个脚本循环运行它。
我用一行简单的代码测试了一下:将"Set B to 2“替换为"Set X to 5”。
然而,您的问题更为复杂,因为您需要替换多行,而不仅仅是1行。我没有找到用多行设置搜索区域的方法。我尝试了CR (13)或LF (10),但它不起作用。也许有人对这一部分有想法?
此外,如果您想在搜索或替换模式中添加“,您可以使用以下方法:
set Guil to ASCII character 34
Set CurString to "this is a " & Guil & "s" & Guil & " between quotes"在本例中,CurString值为:这是引号之间的"s“
发布于 2016-10-22 02:48:34
我从Late Night Software购买了Script Debugger,它使脚本能够访问代码片段并替换它们。Mark Alldritt提供的支持令人惊叹,该软件现在是我的“第一次使用”目的地。
发布于 2016-11-08 05:47:18
你确定你的原始脚本和最终脚本吗?在这种情况下,毫不犹豫地在你写的十六进制脚本中使用xxd和sed,你可以测试这个脚本,对你的脚本没有危险。当然,您可以在方便的时候更改路径和名称。
set thePath to POSIX path of (choose file)
tell application "Script Editor"
set doc to open thePath
save doc as "text" in POSIX file "/Users/yourname/Desktop/yourscriptold.txt"
close thePath
end tell
set scp to do shell script "xxd -p -c 100000 /Users/yourname/Desktop/yourscriptold.txt " & " | sed -e 's#74656c6c206170706c69636174696f6e202241646f62652050686f746f73686f7020434320323031352e35220a736574206d794c6179657220746f2063757272656e74206c61796572206f662063757272656e7420646f63756d656e740a6966206c61737420636861726163746572206f66206d7953706f727420697320227322207468656e0a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f727420262022220a656c73650a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f7274202620222773220a656e642069660a656e642074656c6c#74656c6c206170706c69636174696f6e202241646f62652050686f746f73686f7020434320323031352e35220a736574206d794c6179657220746f2063757272656e74206c61796572206f662063757272656e7420646f63756d656e740a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f7274202620222773220a656e642074656c6c#' > /Users/yourname/Desktop/yourscriptnew.txt"
set scp to do shell script "xxd -r -p /Users/yourname/Desktop/yourscriptnew.txt >/Users/yourname/Desktop/yournewscript.txt"
do shell script "osacompile -o " & "/Users/yourname/Desktop/temporyname.scpt" & " /Users/yourname/Desktop/yournewscript.txt"
do shell script "rm -f /Users/yourname/Desktop/yourscriptold.txt "
do shell script "rm -f /Users/yourname/Desktop/yourscriptnew.txt "
do shell script "rm -f /Users/yourname/Desktop/yournewscript.txt "https://stackoverflow.com/questions/38923769
复制相似问题