【问题标题】:AppleScript to Save Preview DocumentAppleScript 保存预览文档
【发布时间】:2018-07-12 02:53:32
【问题描述】:

我正在使用第三方应用程序将图像复制到剪贴板。然后我想执行一个 AppleScript 打开预览,创建一个新文档,然后将剪贴板用作内容,然后将其保存到指定位置。

我已经完成了 99% 的任务 - 只是无法保存文档。我在 OS X 10.9.5 上。

到目前为止,这是我的脚本:

set the save_location to the quoted form of "/Users/freddy/Documents/test.png"

 tell application "Preview"
    activate
 end tell

 tell application "System Events"
    tell process "Preview"
        keystroke "n" using {command down}
    end tell
 end tell

 tell application "Preview"
    activate
    save front document as "PNG" in POSIX file save_location
 end tell

我找不到保存预览文档的正确语法。这将是当时唯一打开的文档。

【问题讨论】:

    标签: applescript


    【解决方案1】:

    尝试以下方法 - 注意 cmets:

    # Do not use `quoted form of` - only needed and useful with `do shell script`
    set the save_location to "/Users/jdoe/Documents/test.png"
    
    tell application "Preview"
        activate
    end tell
    
    tell application "System Events"
        tell process "Preview"
            keystroke "n" using {command down}
        end tell
    end tell
    
    tell application "Preview"
        # Wait until the new window appears.
        # Trying to save before the window has appeared will fail.
        # Note: - This assumes that NO window was initially open.
        #       - The code should be made more robust to eventually time out.
        repeat until (count of windows) > 0
            delay 0.3
        end repeat
        activate
        # Save the document; `as "<format>"` doesn't seem to work, but the 
        # format is *implied* by the filename suffix (extension).
        save front document in POSIX file save_location
    end tell
    

    【讨论】:

      【解决方案2】:

      此脚本用于将剪贴板中的图像保存到磁盘,在 2010 Mac Mini 上使用 OS 10.11.6 中的预览:

      --CONDITIONS: Finder's clipboard has an image in it
      tell application "Preview"
          launch
          activate
      end tell
      delay 2 --tweak the delays to what works
      tell application "System Events" --not as elegant as "tell application...to tell process", but clearer, chronologically
          keystroke "n" using command down --Preview creates a new window.
          delay 1
          keystroke "v" using command down --Finder's clipboard image is pasted into Preview window.
      end tell
      delay 1
      tell application "System Events"
          set the clipboard to "Joe Blow" --for name of forthcoming Preview file
          delay 1
          keystroke "w" using command down --Forces prompt of Preview to save image.
          delay 1
          keystroke "v" using command down --Pastes new filename into Preview's save window.
          delay 1
          key code 36 --"Return" key saves the new file.
      end tell
      --RESULT: A new image exists in your default folder, named "Joe Blow.png" (or whatever extension)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多