如何使用AppleScript从其POSIX路径中显示Finder中的文件?


6

我正在尝试创建一个AppleScript代码段,用于查找当前的随机壁纸并在Finder中显示它。我有以下代码片段,它将当前壁纸的POSIX路径作为字符串找到:

set plistFolderPath to path to preferences folder from user domain as string
set plistPath to plistFolderPath & "com.apple.desktop.plist"
tell application "System Events"
    tell property list file plistPath
        tell contents
            set thePath to value of property list item "NewChangePath" of property list item "default" of property list item "Background" & "/" & value of property list item "LastName" of property list item "default" of property list item "Background"
        end tell
    end tell
end tell

thePath 现在是一个形式的字符串:

/ Volumes / Archive / Widescreen wallpaper / 12345_Name_2560x1440.jpg

(注意空格)

我尝试在FInder中显示此路径,但我尝试过的所有内容都会导致错误:

tell application "Finder"
    reveal POSIX file of quoted form of thePath (* Error: "Can't get POSIX file of (blah)" *)
end tell

当我拥有的是POSIX路径时,如何在AppleScript中显示Finder中的路径名?

Answers:


9

我认为你的问题是这样的quoted form。尝试这样的事情:

set thePath to POSIX file "/Volumes/Lion HD/Users/ngreenst/Desktop/image.jpg"
tell application "Finder" to reveal thePath

所以就 reveal thePath


如果我像你一样直接将路径粘贴到脚本中,这是有效的,但如果我使用reveal POSIX file thePath它,则返回“无法获取POSIX文件<路径>”错误。
Brant Bobby

1
@BrantBobby这意味着当你收到它时路径有问题。尝试将其转换为字符串(set thePath to ... as string)。如果这不起作用,我将需要确切地看到输出是什么帮助(我无法测试;你的脚本不工作OMM)
Nathan Greenstein

啊,添加as string了诀窍!我想我的假设thePath已经是一个字符串是错误的。
Brant Bobby

4
set p to "/Applications/Utilities/AppleScript Editor.app"

# uses an existing window or makes a new window with your default settings
tell application "Finder"
    reopen # makes a new window if there are no open windows
    activate
    set target of window 1 to (POSIX file p as text)
end tell

# makes a new window that doesn't use your default bounds or view settings
tell application "Finder"
    reveal POSIX file p as text
    activate # focuses the window
end tell
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.