Applescript文件命名

时间:2011-05-23 15:18:48

标签: applescript filenames

代码段:

 tell application "Finder" to set new_item to ¬
 (container of this_item as string) & (name of this_item) & "s"
 save this_image in new_item as typ

将文件保存为filename.pngs我希望它保存为filenames.png。任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

将此保存为应用程序,然后您可以删除它,它将复制它们,在名称中添加一个s。或者您可以双击它,它会要求您选择要复制的文件。 注意:如果要更改添加到名称中的文本(即“s”),请更改属性 appendText

请注意处理程序 getName_andExtension(f)。这就是我用来从文件中获取名称和扩展名的内容。我在添加s时使用它来计算新路径。

我希望有所帮助!

property appendText : "s"

on run
    set f to choose file
    duplicateInPlace_appendingText(f, appendText)
end run

on open the_items
    repeat with f in the_items
        duplicateInPlace_appendingText(f, appendText)
    end repeat
end open

on duplicateInPlace_appendingText(f, textToAppend)
    set f to f as text
    set {nm, ex} to getName_andExtension(f)
    tell application "Finder" to set theContainer to (container of item f) as text

    if ex is "" then
        set new_path to theContainer & nm & textToAppend
    else
        set new_path to theContainer & nm & textToAppend & "." & ex
    end if

    do shell script "cp -R " & quoted form of POSIX path of f & space & quoted form of POSIX path of new_path
end duplicateInPlace_appendingText

on getName_andExtension(f)
    set f to f as text
    set {name:nm, name extension:ex} to info for file f without size
    if ex is missing value then set ex to ""
    if ex is not "" then
        set nm to text 1 thru ((count nm) - (count ex) - 1) of nm
    end if
    return {nm, ex}
end getName_andExtension

答案 1 :(得分:0)

name of this_item提供Finder项的全名,包括文件扩展名。你需要拆分它。不幸的是,这并不是那么容易。你可以这样做:

tell application "Finder"
    get the selection 
    set {dispname, ext, exthidden} to {displayed name, name extension, extension hidden} of item 1 of the result
end tell

if exthidden then
    dispname & "s." & ext
else
    ((characters 1 through -(2 + (count of ext)) of dispname) as string) & "s." & ext
end if

这只是为Finder选择中的第一个项目构建修改后的名称,而不对其进行任何操作。