执行 applescript 时,Finder 应用程序有时会挂起/冻结

Finder application hangs / freezes sometimes when applescript is executed

我是一个 applescript 初学者,我正在尝试在 Finder 中自动执行一些流程。 我的脚本包括一些模拟的鼠标点击 (cliclick)、键码和击键以在 Finder 应用程序中导航。 遗憾的是,在某些情况下 Finder 应用程序有点死机。 一旦我手动点击任何地方,Finder 又是 运行,但突然间所有的键码、击键等都立即执行,没有延迟,导致脚本搞乱了操作。

我知道这个问题的答案或答案对你们中的一些人来说似乎很明显,但我几天前开始使用 applescript,如果有人能帮助我解决这个问题,我将不胜感激。

我已经尝试增加或减少操作之间的延迟,并且我尝试调整 CPU 优先级。可悲的是我无法像那样解决问题。

摘自我的脚本:

repeat 10 times
    delay 2
    key code 48 (* picks first file  *)
    delay 2 (* waits 2 seconds *)
    key code 36 (* press enter to rename file *)
    delay 2 (* waits 2 seconds *)
    key code 124 (* sets Cursor inbetween  filename and file extension *)
    delay 2 (* waits 2 seconds *)
    repeat 5 times
        key code 124 using shift down (* sets cursor one letter to the right and marks letter at the same time, so that the extension is marked after 5 repetitions *)
        delay 2 (* waits 2 seconds *)
    end repeat
    key code 8 using command down (* file extension is copied to the clipboard *)
    delay 2 (* waits 2 seconds *)
    key code 53 (* press escape to escape "rename"- field *)
    delay 2 (* waits 2 second *)
    if ".jpg" = (the clipboard) then (* checks if the file is a jpg *)
        key code 31 using command down (* jpg is opened *)
        delay 3 (* waits 3 seconds *)
        key code 1 using {command down, option down, shift down} (* save image at - window is opened *)
        delay 5 (* waits 2 seconds until window is opened *)
        key code 5 using {shift down, command down} (* open direct data path search windoe *)
        delay 2
        keystroke "/User/abc/def/ghi/jkl/mno/pqr" (* enter data path where image should be safed at *)
        delay 2
        key code 36
        tell application "Terminal"
            do script ("cliclick c:606,625") (* mouseclick formate - jpeg to change it to jpeg2000 in the next step *)
            delay 2 (* wait 2 seconds *)
        end tell
        key code 125 (* selects formate JPG2000 *)
        delay 2 (* waits 2 seconds until new formate/ extension is selected*)
        key code 49 (* press space to confirm the selection *)
        delay 2 (* wait 2 seconds *)
        key code 36 (* press enter to confirm "save at" *)
        delay 5 (* wait 5 seconds until picture is saved in new folder with new extension *)
        key code 12 using command down (* close preview *)
        delay 2
        tell application "Finder" to activate
        delay 2
        key code 51 using command down (* delete first file (was already transferred) *)
        delay 2 (* wait 2 seconds *)
        set the clipboard to "" (* clear clipboard so that .jpg isn't in clipboard anymore *)
        delay 2 (* wait 2 seconds *)
        tell application "Terminal"
            do script ("cliclick c:888,700") (* click anywhere to deselect file *)
            delay 2 (* wait 2 seconds *)
        end tell
    end if
end repeat
end if
end repeat

如果您只想将 jpg 更改为 jpg2000,则无需编写某些应用程序的用户界面脚本。您可以使用 Automator 工作流程:

...如果您真的想使用脚本,Image Events 也会进行转换:

property destination : missing value -- an alternate destination path, for example (path to desktop)

set choices to choose file with prompt "Choose files to convert to JPEG 2000:" with multiple selections allowed
repeat with anItem in choices
    set {basePath, fileName, extension} to getNamePieces from anItem
    try -- check if valid destination
        destination as alias
        set outputPath to (destination as text) & fileName & ".jp2"
    on error -- nope, so use original
        set outputPath to basePath & fileName & ".jp2"
    end try
    try
        tell application "Image Events"
            set theImage to open anItem
            save theImage as JPEG2 in outputPath with icon
            close theImage
        end tell
    on error errmess
        display alert message errmess
    end try
end repeat

to getNamePieces from someItem
    tell application "System Events" to tell disk item (someItem as text)
        set theContainer to the path of container
        set {theName, theExtension} to {name, name extension}
    end tell
    if theExtension is not "" then
        set theName to text 1 thru -((count theExtension) + 2) of theName -- the name part
        set theExtension to "." & theExtension
    end if
    return {theContainer, theName, theExtension}
end getNamePieces