电子邮件中的选定字符串(目录中的文件位置)-使用applescript定位

电子邮件中的选定字符串(目录中的文件位置)-使用applescript定位,applescript,Applescript,如何使用applescript从电子邮件或文档中的文本字符串在finder中查找文件位置 例如:有人给我发电子邮件或发送word文档,其中包含文档的目录路径,我选择了路径。。。如何使用applescript从所选文本字符串中查找“Finder”中的文档 我用它通过硬编码路径来获取文件 set theFile to ("filePath.ext" as POSIX file) tell application "Finder" if (exists theFile) then

如何使用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

我会去建立一个自动化服务。在每个应用程序中将服务输入设置为
文本
。这将从任何应用程序获取当前选定的文本,并将其传递给下一个配置的操作

下一个自动机操作应该是
运行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中打开的服务


干杯,迈克尔/汉堡

如果我将您的代码更改为以下代码,它对我有效

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

结束运行

Hey Michael-我尝试了代码,但它给了我一个错误:“语法错误无法将项目1的段落转换为类型引用。”它给了我一行错误:“将allLinesList设置为(输入的第一项)的段落”啊,好消息:-)在我的测试环境中,一切都正常,选择了多行,Automator得到了一个包含多行输入的字符串。这就是为什么我添加了(输入的第一项)
段落以获得所有行。很好,你找到了解决方案!