【问题标题】:Using applescript, can I change filename without modifying (or getting) the file extension?使用applescript,我可以在不修改(或获取)文件扩展名的情况下更改文件名吗?
【发布时间】:2012-12-25 16:12:04
【问题描述】:

我正在尝试更改文件的名称。如果能够更改“显示名称”属性,这似乎很简单。但我不断收到此错误:

Can't set displayed name of alias "Path:to:file:" to "New_name"

这是我正在使用的文件夹操作脚本(即保存的 applescript,然后使用文件夹操作设置服务将其分配给我的文件夹):

on adding folder items to this_folder after receiving these_items
    try
        repeat with this_item in these_items
            tell application "Finder" to set displayed name of this_item to "New_Name"
        end repeat

    on error error_message number error_number
        display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
    end try
end adding folder items to

我发现的所有执行类似操作的脚本(例如this question)首先获取“名称”属性,然后删除扩展名。我宁愿直接进入“显示名称”属性。

【问题讨论】:

    标签: applescript filenames


    【解决方案1】:

    如果文件具有 Finder 无法识别的扩展名或启用了显示所有扩展名,则显示的名称可以包含扩展名。

    附加上一个扩展不会那么复杂:

    tell application "Finder"
        set f to some file of desktop
        set name of f to "New_name" & "." & name extension of f
    end tell
    

    如果文件没有扩展名或扩展名未被 Finder 识别,这也可以:

    set text item delimiters to "."
    tell application "Finder"
        set f to some file of desktop
        set ti to text items of (get name of f)
        if number of ti is 1 then
            set name of f to "New_name"
        else
            set name of f to "New_name" & "." & item -1 of ti
        end if
    end tell
    

    如果您使用 Automator 创建了文件夹操作,您可以像这样使用 do shell 脚本操作:

    for f in "$@"; do
        mv "$f" "New_name.${f##*.}"
    done
    

    【讨论】:

    • 我打算发布一个简单的检查,如果命名扩展不是“”。但是做了一个虚假扩展的测试。并得到“”。所以谢谢你让我更仔细地看待它。脚本参考。不要解释如果 finder 无法识别扩展名,它将为它返回 ""。因此,在我要发布的普通脚本中,会清除扩展名。 +1
    • 发表我的评论后,我记得系统事件可能会有所不同。它确实看到了我的答案
    【解决方案2】:

    Lauir Ranta 的回答对于 Finder 来说是正确的。

    但在发表我的评论后,我记得 系统事件 比 Finder 更深入地看待事物。

    所以我交换了命令以将名称从 Finder 更改为 System events 所以现在可以了。

    之前,当我有一个名为“someFile.kkl”的文件并且扩展名刚刚组成时。 Finder 将返回 "" 作为扩展名并重命名文件而不使用扩展名。 “新名字”

    但是当系统事件执行它时,它会看到扩展并将名称设置为“newName.kkl”

    tell application "Finder" to set thisFile to (item 1 of (get selection) as alias)
    
    
    tell application "System Events"
        if name extension of thisFile is "" then
    
            set name of thisFile to "newName"
        else
            set name of thisFile to ("newName" & "." & name extension of thisFile)
    
        end if
    
    end tell
    

    在文件夹动作中设置。

    on adding folder items to this_folder after receiving these_items
        try
            repeat with this_item in these_items
                tell application "System Events"
                    if name extension of this_item is "" then
    
                        set name of this_item to "new_Name"
                    else
                        set name of this_item to ("new_Name" & "." & name extension of this_item)
    
                    end if
    
                end tell
            end repeat
    
        on error error_message number error_number
            display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
        end try
    end adding folder items to
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-18
      • 2015-10-13
      • 1970-01-01
      • 2023-03-21
      • 2022-11-11
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多