开发者

Applescript File naming

开发者 https://www.devze.com 2023-03-08 00:40 出处:网络
Code snippet: tell application \"Finde开发者_运维知识库r\" to set new_item to ¬ (container of this_item as string) & (name of this_item) & \"s\"

Code snippet:

 tell application "Finde开发者_运维知识库r" to set new_item to ¬
 (container of this_item as string) & (name of this_item) & "s"
 save this_image in new_item as typ

Its saving the file as filename.pngs I want it to save as filenames.png. Any ideas how to fix this?


Save this as an application, then you can drop things on it and it will copy them adding an s to the name. Or you can double-click it and it will ask you to choose a file to copy. NOTE: change the property appendText if you want to change the text (i.e. "s") that's added to the name.

Notice the handler getName_andExtension(f). That's what I use to get the name and extension from a file. I use that to calculate the new path when adding the s.

I hope that helps!

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


name of this_item gives the full name of the Finder item, including the file extension. You need to split it up. Unfortunately, this isn't as easy as it could be. You could do something like:

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

That just builds the modified name for the first item in the Finder selection, without doing anything with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消