【问题标题】:Finder application hangs / freezes sometimes when applescript is executed执行 applescript 时,Finder 应用程序有时会挂起/冻结
【发布时间】:2019-09-04 23:36:46
【问题描述】:

我是一个 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

【问题讨论】:

  • 听起来您的脚本通过在循环中执行某些事情来阻止 UI,这不会让系统正常处理事件,但是如果没有具体看到您在做什么,这将很难判断。跨度>
  • 好的,请等几分钟。我马上给你看脚本
  • GUI 脚本是那些“不得已而为之,没有别的办法”之类的东西之一,但即便如此,您也应该尝试寻找另一种方法。这看起来像是某种 XY 问题 - Finder 是可编写脚本的,你想做什么?
  • 在脚本的开头,我有几张jpg,无法在photoshop中处理。因此,我试图将它们一个一个地转换为 jpg2000s。在脚本结束时,它们都应该保存到 Photoshop 可以从中导入图像的特定文件夹中。我希望你能理解一切????
  • 正如我所说,我是 applescript 和一般脚本的新手。我认为,与使用 Finder 命令相比,像这样转换图像的过程会更容易。 Finder 真的可以完成所有这些操作吗?如果我问:D 该怎么做?

标签: macos applescript automator


【解决方案1】:

如果您只想将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

【讨论】:

  • 哇,我没想到会这么简单 :D 。非常感谢您的回答!
猜你喜欢
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多