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

时间:2018-10-30 15:10:18

标签: macos shell applescript zsh clipboard

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

我尝试过IF lo_data IS NOT BOUND. EXIT. ENDIF. ,它只是返回其中一个复制文件的基本名称。

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

2 个答案:

答案 0 :(得分:1)

在看不到有关复制的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)

OR

由于您的过程是使用键盘快捷键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

enter image description here

enter image description here

存储文件路径信息后,您将使用快捷键cmd + c复制查找程序项目,并对复制的查找程序文件执行任何操作。

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

enter image description here

答案 1 :(得分:1)

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

但是,在其他情况下,则需要从剪贴板中检索文件。下面的AppleScript片段将使用 Finder 中的 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 ..."

或者您可以使用 Script Editor 将AppleScript保存为文件,扩展名为.scpt(编译代码)或.applescript(文本),然后从命令中运行像这样的线:

osascript /path/to/file.applescript