2015-03-31 115 views
0

我正在使用第三方應用程序將圖像複製到剪貼板。然後,我想執行一個打開Preview的AppleScript,創建一個新文檔,然後使用剪貼板作爲內容,然後將其保存到指定的位置。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 

我無法找到正確的語法,用於保存預覽文件。這將是當時唯一開放的文件。

回答

1

試試下面的 - 注意評論:

# 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