使用applescript,我可以在不修改(或获取)文件扩展名的情况下更改文件名吗?

使用applescript,我可以在不修改(或获取)文件扩展名的情况下更改文件名吗?,applescript,filenames,Applescript,Filenames,我正在尝试更改文件名。如果能够更改“显示的名称”属性,则看起来很简单。但我一直在犯这样的错误: 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

我正在尝试更改文件名。如果能够更改“显示的名称”属性,则看起来很简单。但我一直在犯这样的错误:

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

我发现的所有脚本都做了类似的事情,例如,首先获取name属性,然后剥离扩展。我宁愿直接转到displayed name属性。

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

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

tell application "Finder"
    set f to some file of desktop
    set name of f to "New_name" & "." & name extension of f
end tell
如果文件没有扩展名,或者查找程序无法识别扩展名,也可以这样做:

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无法识别的扩展名,或者启用了“显示所有扩展名”,则显示的名称可以包含扩展名

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

tell application "Finder"
    set f to some file of desktop
    set name of f to "New_name" & "." & name extension of f
end tell
如果文件没有扩展名,或者查找程序无法识别扩展名,也可以这样做:

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更改为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

劳尔·兰塔的回答对发现者来说是正确的

但是在发表了我的评论之后,我记得系统事件比查找器看得更深一些

因此,我交换了命令,将名称从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

我打算发布一个简单的检查,看看命名扩展是否正确。但是做了一个假扩展测试。并且得到了。所以谢谢你让我更仔细地看这一点。脚本引用。不要解释如果finder不识别扩展,它将返回该扩展。因此,在我将要发布的普通脚本中,会删除扩展+1发布我的评论后,我想起系统事件可能会有不同的表现。它确实看到了我的答案。我打算发布一个简单的检查,看看命名扩展是否正确。但是做了一个假扩展测试。并且得到了。所以谢谢你让我更仔细地看这一点。脚本引用。不要解释如果finder不识别扩展,它将返回该扩展。因此,在我将要发布的普通脚本中,会删除扩展+1发布我的评论后,我想起系统事件可能会有不同的表现。它确实看到了我的答案