当执行applescript时,Finder应用程序有时会挂起/冻结 [英] Finder application hangs / freezes sometimes when applescript is executed

查看:170
本文介绍了当执行applescript时,Finder应用程序有时会挂起/冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是applescript初学者,我正在尝试使Finder中的某些过程自动化. 我的脚本包括一些模拟的鼠标单击(cliclick),键代码和击键,以在Finder应用程序中导航. 不幸的是,在某些情况下 Finder应用程序冻结了. 一旦我手动单击任何地方,Finder就会再次运行,但是突然所有的键代码,击键等都立即执行,没有任何延迟,导致脚本弄乱了动作.

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.

我知道这个问题的答案或灵感在你们中的某些人看来似乎很明显,但是几天前我从applescript开始,如果有人可以帮助我解决这个问题,我将非常感激.

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.

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

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.

摘录自我的脚本:

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工作流程:

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

这篇关于当执行applescript时,Finder应用程序有时会挂起/冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆