我如何使用applescript从电子邮件或文档中的文本字符串中查找finder中的文件位置?
例如:有人通过电子邮件或向我发送带有文档目录路径的word文档,然后选择路径...我将如何使用applescript在" Finder"中查找文档?从选定的文本字符串?
我通过硬编码路径来使用它来获取文件...
set theFile to ("filePath.ext" as POSIX file)
tell application "Finder"
if (exists theFile) then
select theFile
activate
else
display alert "File " & theFile & " does not exist"
end if
end tell
答案 0 :(得分:0)
我会去构建 Automator服务。将服务'输入设置为Text
和in every Application
。这将从任何应用程序中获取当前选定的文本,并将其传递给下一个已配置的操作。
下一个Automator操作应该是run Applescript
工作流程步骤。填写
on run {input, parameters}
-- in case that multiple lines are selected
set allLinesList to paragraphs of (first item of input)
-- for future needs: output only valid file paths
set validFilePaths to {}
-- walking through the lines and try to reveal the file in Finder
repeat with aLine in allLinesList
try
set targetAlias to POSIX file aLine as alias
tell application "Finder" to reveal targetAlias
set end of validFilePaths to {}
-- commented out, needed for debugging only:
--on error errstr
-- display dialog errstr
end try
end repeat
-- give valid paths only to the next action
return validFilePaths
end run
如果您愿意,可以将此AppleScript用作过滤器,因为它只将有效的文件路径传递给下一个操作。
根据您的需要,您可能需要进行一些更正。即您可能希望删除所选字符串前面和末尾的空格。
将其保存为在Finder中打开,每次在应用程序中选择文本时,您都会在上下文菜单中找到服务在Finder中打开。
干杯,迈克尔/汉堡
答案 1 :(得分:0)
如果我将您的代码更改为以下内容,则对我有用...
on run {input, parameters}
-- in case that multiple lines are selected
set allLinesList to input
-- for future needs: output only valid file paths
set validFilePaths to {}
-- walking through the lines and try to reveal the file in Finder
repeat with aLine in allLinesList
try
set targetAlias to POSIX file aLine as alias
tell application "Finder" to reveal targetAlias
set end of validFilePaths to {}
-- commented out, needed for debugging only:
--on error errstr
-- display dialog errstr
end try
end repeat
-- give valid paths only to the next action
return validFilePaths
结束