【问题标题】:Getting the file name of files dropped on the script获取脚本中放置的文件的文件名
【发布时间】:2011-02-24 09:36:45
【问题描述】:

我制作了这个 Applescript 脚本来创建符号链接。
Appart from POSIX path of,如何获取删除文件的文件名,没有路径?

on open filelist
    repeat with i in filelist
        do shell script "ln -s " & POSIX path of i & " /Users/me/Desktop/symlink"
    end repeat
end open

PS:我知道这需要删除许多文件并尝试创建许多具有相同名称的链接,这会产生错误。实际上,我从一个网站复制了这个示例,并且由于我对 Applescript 几乎一无所知,我不知道如何为单个文件执行此操作,对此也将不胜感激。

【问题讨论】:

    标签: macos path applescript posix symlink


    【解决方案1】:

    我不确定你到底想做什么,但我有一个猜测。您是否想要获取脚本上的每个文件并为桌面上的每个文件创建一个符号链接?所以如果我放弃~/look/at/me 和~/an/example,你会得到~/Desktop/me 和~/Desktop/example?如果这就是您想要的,那么您很幸运:ln -s <file1> <file2> ... <directory> 正是这样做的。 (编辑:虽然您必须注意两个参数的情况。)因此,您的代码可能如下所示:

    -- EDITED: Added the conditional setting of `dest` to prevent errors in the
    -- two-arguments-to-ln case (see my comment).
    
    on quoted(f)
        return quoted form of POSIX path of f
    end quoted
    
    on open filelist
        if filelist is {} then return
        set dest to missing value
        if (count of filelist) is 1 then
            tell application "System Events" to set n to the name of item 1 of filelist
            set dest to (path to desktop as string) & n
        else
            set dest to path to desktop
        end if
        set cmd to "ln -s"
        repeat with f in filelist & dest
            set cmd to cmd & " " & quoted(f)
        end repeat
        do shell script cmd
    end open
    

    注意quoted form of的使用;它将参数用单引号括起来,因此在 shell 中执行不会做任何有趣的事情。

    如果您出于其他原因想要获取文件名,则无需调用 Finder;您可以改用系统事件:

    tell application "System Events" to get name of myAlias
    

    将返回存储在myAlias中的文件名。


    编辑:如果你想对单个文件做一些事情,这很容易。不要使用repeat 遍历每个文件,只需对item 1 of theList 访问的第一个文件执行相同的操作。所以在这种情况下,你可能想要这样的东西:

    -- EDITED: Fixed the "linking a directory" case (see my comment).
    
    on quoted(f)
        return quoted form of POSIX path of f
    end quoted
    
    on open filelist
        if filelist is {} then return
        set f to item 1 of filelist
        tell application "System Events" to set n to the name of f
        do shell script "ln -s " & ¬
            quoted(f) & " " & quoted((path to desktop as string) & n)
    end open
    

    几乎相同,但我们抓取filelist 中的第一项并忽略其余部分。此外,最后,我们会显示一个包含符号链接名称的对话框,以便用户知道刚刚发生了什么。

    【讨论】:

    • 谢谢,我真正想做的是单个文件。我使用了重复,因为我从网站上获得了该示例,并且不知道如何为单个文件修改它。
    • 编辑:它对文件很有效,但对文件夹无效,这是我最想链接的。错误信息是:“在:/Users/petruza/Desktop/:文件存在”桌面上没有与原始文件同名的文件,这适用于常规文件,因此路径是正确的。似乎当在脚本上放置一个文件夹时,它会将其解释为“/”,这是错误消息中添加到桌面路径的内容。如果我在路径中添加斜杠,错误会显示两个斜杠。
    • 我觉得自己学applescript会更好。你能推荐一个好的教程吗?
    • 哦,废话,它不起作用是我的错——如果您尝试将单个文件夹链接到一个文件夹,它会尝试替换它,而您不希望这样。让我解决它!
    • 好的,已修复。至于你的另一个问题:对你有好处主动学习语言!我实际上并不确定自己(我通过实验学习,这是一个很好的计划),但我会发布一个单独的问题 - 应该有普遍的兴趣。
    【解决方案2】:

    例如,您可以使用 Finder 而不是 shell 脚本来获取单个文件的名称,该文件被拖放到另存为应用程序的脚本上。如果您不需要显示对话框,可以将其删除,但您可以将文件名作为变量使用:

    on open the_files
        repeat with i from 1 to the count of the_files
            tell application "Finder"
                set myFileName to name of (item i of the_files)
            end tell
            display dialog "The file's name is " & myFileName
        end repeat
    end open
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2020-11-03
      • 2011-11-02
      • 2015-05-24
      • 1970-01-01
      相关资源
      最近更新 更多