问题描述
我是一个 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:
...如果您真的想使用脚本,Image Events 也会进行转换:
...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 应用程序有时会挂起/冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!