Macos shell:检索Finder复制的文件的路径

Macos shell:检索Finder复制的文件的路径,macos,shell,applescript,zsh,clipboard,Macos,Shell,Applescript,Zsh,Clipboard,我想在zsh脚本中获得Finder复制的文件路径。我不介意调用外部实用程序/脚本 我尝试了pbpaste,它只返回一个复制文件的基本名称 PS:文件是用cmd+c在Finder中手动复制的。如果看不到有关复制的Finder项的代码,很难为您提供准确的解决方案。这里有一个可能的解决方案。下面的AppleScript代码将获取当前选定的查找程序项,并将剪贴板设置为这些选定查找程序项的完整文件路径(作为列表) tell application "Finder" to set selectedFiles

我想在zsh脚本中获得Finder复制的文件路径。我不介意调用外部实用程序/脚本

我尝试了
pbpaste
,它只返回一个复制文件的基本名称


PS:文件是用cmd+c在Finder中手动复制的。

如果看不到有关复制的Finder项的代码,很难为您提供准确的解决方案。这里有一个可能的解决方案。下面的AppleScript代码将获取当前选定的查找程序项,并将剪贴板设置为这些选定查找程序项的完整文件路径(作为列表)

tell application "Finder" to set selectedFiles to selection as alias list
if selectedFiles is {} then return
set filePaths to {}
repeat with thisFile in selectedFiles
    set end of filePaths to POSIX path of thisFile
end repeat
set text item delimiters to linefeed
set the clipboard to (filePaths as text)

由于您的过程是使用键盘快捷键cmd+c将选定的查找程序文件复制到剪贴板,因此您可以在Automator.app中使用以下AppleScript代码并将其另存为服务。然后,在System Preferences.app中,您可以为新服务分配键盘快捷键

在使用键盘快捷键cmd+c复制Finder项之前,您需要运行刚刚创建的服务来存储文件路径,以便以后检索它们

简而言之,此AppleScript代码将显示一个对话框,为您提供两个选项

1) 将当前选定查找程序文件的文件路径写入临时文本文件(需要时可以检索)

2) 通过从临时文件中检索该信息,将剪贴板设置为文件路径

tell application "Finder" to set selectedFiles to selection as alias list
if selectedFiles is {} then return
set filePaths to {}
repeat with thisFile in selectedFiles
    set end of filePaths to POSIX path of thisFile
end repeat
set text item delimiters to linefeed
set filePaths to (filePaths as text)

set readOrWrite to {"Write Selected Files' File Path To Temp", "Set Clipboard To The File Paths"}

activate
set theChoice to (choose from list readOrWrite ¬
    with title "CHOOSE YOUR OPTION PLEASE" with prompt ¬
    "Write File Paths Or Set Clipboard To File Paths?" default items 1 ¬
    OK button name "DO IT" cancel button name "Cancel") as string

if theChoice is "Write Selected Files' File Path To Temp" then
    writeToFile(filePaths)
else if theChoice is "Set Clipboard To The File Paths" then
    readFile()
else
    return
end if


on writeToFile(filePaths)
    set theFile to "/tmp/File_Paths.txt"
    set theText to filePaths
    try
        set writeToFile to open for access theFile with write permission
        set eof writeToFile to 0
        write theText & linefeed to writeToFile as text starting at eof
        close access theFile
    on error errMsg number errNum
        close access theFile
        set writeToFile to open for access theFile with write permission
        set eof writeToFile to 0
        write theText & linefeed to writeToFile as text starting at eof
        close access theFile
    end try
end writeToFile

on readFile()
    set theFile to "/tmp/File_Paths.txt"
    set the clipboard to (read theFile)
end readFile

存储文件路径信息后,您可以使用快捷方式cmd+c复制查找程序项,对复制的查找程序文件执行任何操作

然后返回并再次运行该服务,将文件路径复制到剪贴板


您可以使用Finder将完整文件路径复制到剪贴板⌘⌥C

但是,在其他情况下,需要从剪贴板检索文件。下面的AppleScript片段将检索上次使用复制到剪贴板的文件集⌘C在查找器中。最初,它检索文件对象本身,可以在AppleScript中以各种方式使用这些对象。但是,脚本的后半部分将文件对象转换为一个简单的posix文件路径列表,然后将其连接成一个由换行符分隔的字符串

use framework "AppKit"

property this : a reference to current application
property NSPasteboard : a reference to NSPasteboard of this
property NSURL : a reference to NSURL of this

property text item delimiters : linefeed

set pb to NSPasteboard's generalPasteboard()
set fs to (pb's readObjectsForClasses:[NSURL] options:[]) as list

repeat with f in fs
    set f's contents to POSIX path of f
end repeat

fs as text
要在
zsh
中实现这一点,您可以像这样使用
osascript

osascript -e "use framework \"AppKit\"

property this : a reference to current application
property NSPasteboard ..."
或者,您可以使用扩展名为
.scpt
(编译代码)或
.AppleScript
(文本)的脚本编辑器将AppleScript保存为文件,然后从命令行运行它,如下所示:

osascript /path/to/file.applescript

你能发布applescript和zsh脚本的相关部分吗?现在我们只能猜测。我猜您使用了类似于
move[…]to folder
,因此在能够使用
pbpaste
之前,您必须添加类似于
将剪贴板设置到folder
的内容。谢谢,我现在可以用它来解决我的用例,但这并不能回答问题。我想获取已在Finder中复制的文件的路径,而不是选定的路径。我将给出另一个函数的路径,该函数将PSD文件转换为PNG。类似于
cpsd“$(粘贴路径)”
。这就是我的观点。如果没有看到您的代码,就很难提供准确的答案。如何复制查找文件?作为一个手工过程?在finder中选择它们并用键盘快捷键复制?是的,准确地说:))我只是在finder中选择文件并执行cmd+c。在旁注中,您的applescript告诉我“执行错误:查找程序出错:应用程序未运行。(-600)”。您是否知道选项+cmd+c。。作为选定查找程序项的快捷键,是否会复制完整路径名?我不知道(我知道,但不知道,不知道:D),但我仍然希望得到问题的答案。