代码之家  ›  专栏  ›  技术社区  ›  DRedhorse

在osx-automator中获取pdf打印对话框的文件名

  •  0
  • DRedhorse  · 技术社区  · 10 年前

    我正在创建一个自动pdf打印插件。

    当您选择打印插件时,pdf的文件名是输入(通常为/var/something/documentName.pdf)

    我希望稍后在重命名查找器项中使用documentName。

    我正在使用atm应用描述来实现这一点。

    on run {input, parameters}
    
    tell application "Finder"
        set fileName to name of ((POSIX file input) as alias)
    end tell
    return fileName as string
    
    end run
    

    问题是,只有当我将Ask for Text Action放在显示posix路径的appdescriptionpt之前时,这才有效。

    如果我删除了Ask for Text操作,则applecript将失败。

    工作流位于 https://www.dropbox.com/s/jp4t9pen3gvtyiq/Rename-Action.workflow.zip

    我想这很简单,但这是我正在创建的第一个应用描述/自动化工作流。

    因为我没有评论

    解决方案是

    on run {input, parameters}
    
    tell application "Finder"
        set fileName to ((name of first item of input) as string)
    end tell
    return fileName 
    end run
    

    正如下面的@Ken帖子所述。

    谢谢

    3 回复  |  直到 10 年前
        1
  •  1
  •   Ken Thomases    10 年前

    我使用此AppleScript创建了测试工作流:

    on run {input, parameters}
        tell app "System Events"
            display dialog ((class of input) as string)
        end
        return input
    end run
    

    显示“列表”。然后我将其修改为:

    on run {input, parameters}
        tell application "System Events"
            display dialog ((class of first item of input) as string)
        end tell
        return input
    end run
    

    显示“别名”。

    因此,PDF工作流的输入是别名列表。写你的剧本时要记住这一点,它应该会起作用。例如,这可以:

    on run {input, parameters}
        tell application "System Events"
            display dialog ((name of first item of input) as string)
        end tell
        return input
    end run
    
        2
  •  0
  •   Simon White    10 年前

    在使用AppleScript时,它确实可以帮助您忘记有关文件路径的所有信息。如果你在路径上思考,你就会在脑子里做路径数学,所有这些都是不必要的工作。您要处理的是对象。执行文件操作时,将使用别名对象。

    如果您在Finder中查看正在使用的PDF文件,然后转到“文件”“生成别名”,那么您将创建一个别名文件。您可以将该别名文件拖到磁盘的文件系统周围,将其放在任何文件夹中,当您打开该别名文件时,它仍将始终打开原始PDF,即使您忘记了原始PDF文件的路径名,更重要的是:即使PDF已移到文件系统的其他位置,该别名也将打开PDF。别名为您完成所有工作。你不需要知道路径名。

    在AppleScript中,您使用的不是文件,而是别名,对别名所做的任何操作也会对原始文件执行。因此,您不需要知道文件的路径名就可以更改其名称,只需要使用文件的别名即可。您可以将该别名存储在变量中。

    因此,您要做的是将输入的PDF别名设置为一个变量,然后,该变量就是您给Finder重命名的变量。你不必知道任何路径。输入的PDF存储在文件系统的何处并不重要,别名将负责这一点。

    下面是一个示例AppleScript,它演示了将别名作为输入,然后重命名该别名(因此,原始文件:)的原理

    tell application "Finder"
        set theInputFile to (choose file)
        -- do a workflow here
        set the name of theInputFile to "Renamed" & "." & the name extension of theInputFile
    end tell
    

    以下是上述脚本的逐行描述:

    1. 指定我们正在与Finder对话的开头提示块
    2. 向用户显示一个选择文件对话框,并将该对话框返回的别名设置为名为InputFile的变量
    3. 注释,它是您可能要执行的任何工作流步骤的占位符
    4. 将InputFile重命名为Renamed及其原始文件扩展名
    5. 停止与Finder交谈

    即使您想使用包含原始输入文件的文件夹,或者想知道输入文件所在的磁盘,也不需要使用路径名:

    tell application "Finder"
        set theInputFile to (choose file)
        set theContainingFolder to open the container of theInputFile
        set theInputFileDisk to the disk of theInputFile
    end tell
    

    如果你想知道输入文件是什么类型的文件,你不必查看文件扩展名并找出它,你可以说:

    set theInputFileKind to the kind of theInputFile
    if theInputFileKind is equal to "Portable Document Format (PDF)" then
        -- do stuff
    end if
    

    如果要在特定文件夹(如主文件夹)中工作,则有一些特殊的财产,如主文件夹的路径,以便以下脚本在任何系统上打开~/Public/Drop-Box,而不管用户名是什么:

    tell application "Finder"
        activate
        set theHomeFolder to the path to the home folder as alias
        set theDropBoxFolder to folder "Drop Box" of folder "Public" of theHomeFolder
        open theDropBoxFolder
    end tell
    

    您可以像上面所示的那样,将磁盘和文件夹结构作为对象进行遍历,因此,也无需考虑路径。考虑将变量设置为要与之交互的对象。

        3
  •  0
  •   DRedhorse    10 年前

    解决方案是

    on run {input, parameters}
    
        tell application "Finder"
           set fileName to ((name of first item of input) as string)
        end tell
        return fileName 
    end run
    

    通过@Ken的帖子

    谢谢