Finder application hangs / freezes sometimes when applescript is executed

给你一囗甜甜゛ 提交于 2019-12-08 12:35:25

问题


I am an applescript beginner and I am trying to automate some processes in the Finder. My script includes some simulated mouseclicks (cliclick), key codes and keystrokes to navigate through the Finder application. Sadly, in some cases the Finder application kind of freezes. As soon as i click on anywhere manually, Finder is running again but suddenly all the key codes, keystrokes etc. are executed at once without delays, leading the script to mess up the actions.

I know that the answer or soultion to this question might seem obvious to some of you, but I started with applescript a few days ago and I would be very thankful if somebody could help me with this problem.

I have already tried increasing or decreasing the delay inbetween actions and I have tried to adjust the CPU priorities. Sadly i couldn't fix the problem like that.

excerpt from my script:

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

回答1:


If all you are wanting to do is change a jpg to a jpg2000, there is no need to script the user interface of some application. You can use an Automator workflow:

...and if you really want to use a script, Image Events will also do the conversion:

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


来源:https://stackoverflow.com/questions/57796743/finder-application-hangs-freezes-sometimes-when-applescript-is-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!