使用applescript,我可以在不修改(或获取)文件扩展名的情况下更改文件名吗? [英] Using applescript, can I change filename without modifying (or getting) the file extension?

查看:34
本文介绍了使用applescript,我可以在不修改(或获取)文件扩展名的情况下更改文件名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改文件名.看起来很简单,如果能够更改显示名称"属性.但我不断收到此错误:

I am trying to change the name of a file. Seems simple enough, if one is able to change the "displayed name" property. But I keep getting this error:

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

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

Here's the folder action script I'm using (i.e. saved applescript, then used folder action setup service to assign it to my folder):

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

我发现的所有执行类似操作的脚本(例如这个问题)首先获得名称"属性然后剥离扩展.我宁愿直接进入显示名称"属性.

All the scripts I'm finding that does something similar (e.g. this question) first get the "name" property then strip the extension. I'd rather just go straight to the "displayed name" property.

推荐答案

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

The displayed name can contain the extension if the file has an extension that isn't recognized by Finder or if showing all extensions is enabled.

追加以前的扩展名不会那么复杂:

Appending the previous extension wouldn't be that complex:

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

如果文件没有扩展名或者 Finder 无法识别该扩展名,这也适用:

This would also work if the file has no extension or if the extension isn't recognized by 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 脚本操作:

If you created the folder action with Automator, you could use a do shell script action like this:

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

这篇关于使用applescript,我可以在不修改(或获取)文件扩展名的情况下更改文件名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆