首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AppleScript保存预览文档

AppleScript保存预览文档
EN

Stack Overflow用户
提问于 2015-03-31 03:32:14
回答 2查看 2K关注 0票数 0

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

我有99%的路要去--只是拿不到文件来保存。我在OS 10.9.5上。

到目前为止,我的剧本如下:

代码语言:javascript
运行
复制
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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-31 04:26:48

试着做以下几点--注意注释:

代码语言:javascript
运行
复制
# 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
票数 1
EN

Stack Overflow用户

发布于 2018-07-11 14:46:30

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

代码语言:javascript
运行
复制
--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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29360133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档