我在Finder中选择了一个或多个文件和/或文件夹。我手动将它们复制到剪贴板/粘贴板(⌘C).

为了使事情变得简单,假设我只是复制了一个普通文件。但是,理想的解决方案将处理许多文件和与别名的文件夹混合选择。

现在这个文件在剪贴板上,我想获得其完整路径 (最好是POSIX路径)。


为您节省时间:

  • 我正在寻找一个AppleScript(或RB-AppScript)解决方案。
  • 我不想直接从选择中获得路径。它必须来自剪贴板上的项目。
  • 真的,我知道我可以通过将路径复制到选择,然后做我做的任何事情来绕过这一点。

到目前为止,我所知道的(在RB-Appscript中指出):

  • OSAX.osax.the_clipboard 有一个没有路径的文件名。
  • Appscript.app('Finder').clipboard.get 显然没有实施(字典说“尚未可用”;称其为返回 :missing_value.
有帮助吗?

解决方案

以下AppleScript似乎可以解决问题:

POSIX path of (the clipboard as «class furl»)

如果剪贴板上有多个项目,它将仅返回第一个项目的POSIX路径。

也看到 AppleScript命令参考 为命令 the clipboard.


RB-AppScript版本:

OSAX.osax.the_clipboard(:result_type => :file_url).path

其他提示

这是一个applescript,它将从剪贴板中获取所有POSIX路径,而不仅仅是第一个...

set theFiles to paragraphs of (get the clipboard)

set posixPaths to {}
repeat with aFile in theFiles
    try
        tell application "Finder" to set thePath to item aFile as text
        set end of posixPaths to (POSIX path of thePath)
    end try
end repeat
return posixPaths

只是以为我会分享我在萨克拉(Sakra)答案之后写的RB-appscript代码:

#!/usr/bin/arch -i386 /usr/bin/ruby
require "rubygems"
require "osax"
include OSAX


def path_from_clipboard
  osax.clipboard_info.flatten.include? :file_url or raise "clipboard does not contain path data" 
  osax.the_clipboard.count("\r") == 0            or raise "clipboard contains more than one path"
  osax.the_clipboard(:result_type => :file_url).path
end

puts path_from_clipboard

发现器就是它的本质,而applescript就是它,这一切都无法解决。所以,我到底跳进可可。

这些脚本中的任何一个都将返回新行上的绝对路径列表。

麦克劳比:

#!/usr/bin/env macruby
# encoding: UTF-8
framework 'Cocoa'
puts NSPasteboard.generalPasteboard.pasteboardItems
  .map { |pbi| pbi.stringForType('public.file-url') }.compact
  .map { |url| NSURL.URLWithString(url).path }

nu:

#!/usr/bin/env nush
(puts ((((((NSPasteboard generalPasteboard) pasteboardItems)
  map:    (do (pbi) (pbi stringForType: "public.file-url")))
  select: (do (url) (url)))
  map:    (do (url) ((NSURL URLWithString: url) path))) componentsJoinedByString: "\n"))

我正在寻找一种解决方案,该解决方案将复制Finder中选择的文件的路径。这是我想到的:

set ASTID to AppleScript's text item delimiters --——>>
set AppleScript's text item delimiters to return

tell application "Finder" to set sel to the selection as text

set listPaths to {}
repeat with pth in paragraphs of sel
    set end of listPaths to POSIX path of pth
end repeat

set listPathsClipboard to listPaths as text
set AppleScript's text item delimiters to ASTID --——<<

set the clipboard to listPathsClipboard
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top