如何在mac预览版10.7中重新加载文件?

kef*_*ich 5 macos applescript preview osx-lion

我想在更新后重新加载预览中的文件(.png,.pdf).如何实现这一目标?

在OS X 10.5和10.6中,它就像切换到预览一样简单 - 它会自动重新加载新文件.或者,您可以使用open -a Preview *.png或类似的东西.在10.7中,自动重新加载不起作用(参见本文).

我的第一次尝试是从命令行运行的applescript脚本:

/usr/bin/osascript -e 'tell application "Preview.app" to activate
    tell application "Preview.app" to open "'$PWD/$*'"' 
Run Code Online (Sandbox Code Playgroud)

这适用于单个文件,但由于显而易见的原因而无法使用多个文件.我做了一些研究,并尝试使用更复杂的Applecript涉及set和列表,但这会导致权限错误:

使用高级脚本时出现错误

这是我使用的python脚本(我的bash脚本技能不符合字符串解析的任务):

#!/usr/bin/env python

import optparse
import os

parser=optparse.OptionParser()
options,args = parser.parse_args()

pwd = os.getcwd()

cmd = '/usr/bin/osascript -e '
scriptcmd = "tell application \"Preview.app\" to activate\n" 

flist = [ fn  if fn[0]=='/' else pwd+"/"+fn  for fn in args]

scriptcmd += "set myList to {\"%s\"}\n" % ( '","'.join(flist) )

scriptcmd += "tell application \"Preview.app\" to open myList"

print("%s \'%s\'" % (cmd,scriptcmd))

os.system("%s \'%s\'" % (cmd,scriptcmd))
Run Code Online (Sandbox Code Playgroud)

我甚至不确定这个脚本是否能解决我原来的问题 - 重新加载图像而不会看到灰色屏幕 - 但我想知道是否有任何方法只需打开一个文件列表osascript而不是open.

编辑:尝试修复applescript,但此代码出现"缺失值"错误:

tell application "Preview.app" to activate
set myListOfImages to {":Users:adam:work:code:test.png"} 
tell application "Preview.app" to open myListOfImages
Run Code Online (Sandbox Code Playgroud)

reg*_*633 1

这是一个在 10.7 中适用于我的简单 applescript。

set picsFolder to (choose folder with prompt "Choose the folder to search...") as text

tell application "Finder"
    set theImages to (files of folder picsFolder whose name extension is "jpg") as alias list
end tell

tell application "Preview"
    activate
    open theImages
end tell
Run Code Online (Sandbox Code Playgroud)

我在您的帖子中列出的 osascript 命令中看到一些错误。首先,osascript 命令的格式不正确。其次,您尝试在 applescript 命令中使用 posix 样式路径(例如斜杠分隔)。Applescript 需要 applescript 样式的路径(例如,冒号分隔)。还有其他问题。不管怎样,这里有一个 osascript,您可以从命令行运行它,它的工作原理是像您尝试做的那样搜索当前工作目录......

/usr/bin/osascript -e 'set posix_picsFolder to do shell script "PWD"' -e 'set picsFolder to (POSIX file posix_picsFolder) as text' -e 'if picsFolder does not end with ":" then set picsFolder to picsFolder & ":"' -e 'tell application "Finder" to set theImages to (files of folder picsFolder whose name extension is "jpg") as alias list' -e 'tell application "Preview"' -e 'activate' -e 'open theImages' -e 'end tell'
Run Code Online (Sandbox Code Playgroud)

注意:我的代码查找 jpg 文件,因此只需将 jpg 更改为 png 或您想要的任何其他文件扩展名即可。

编辑:回答评论中的其他问题

如果您想搜索多个扩展名,您可以使用“或”,如下所示......

tell application "Finder"
    set theImages to (files of folder picsFolder whose name extension is "jpg" or name extension is "png") as alias list
end tell
Run Code Online (Sandbox Code Playgroud)

要找到正确的“:”分隔路径,这里有一个简短的苹果脚本,它将向您显示它们。如果您想要“文件夹”路径而不是“文件”,只需更改代码中的单词即可。您会注意到这些样式路径始终以您的硬盘驱动器名称开头。

set colonDelimitedPath to (choose file) as text
Run Code Online (Sandbox Code Playgroud)

如果您想将 posix 样式路径转换为 ​​applescript 样式路径,请使用此路径。

set posixPath to "/Applications/"
set macPath to (POSIX file posixPath) as text
Run Code Online (Sandbox Code Playgroud)