2011-05-23 77 views
-1

代碼片段:AppleScript的文件命名

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

將其另存爲應用程序,然後您可以在其上放置東西,並將其複製到名稱中並添加s。或者您可以雙擊它,它會要求您選擇要複製的文件。 備註:更改屬性appendText如果您想更改添加到名稱的文本(即「s」)。

注意處理程序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 
+0

這有助於很多。我希望我不要求太多,但無論如何,你可以將它與這個合併:http://hints.macworld.com/article.php?story=2004092804461334?我想拖動多個(不包括在我包括的調整大小腳本)圖像,並添加s(小)名稱,一旦它的大小調整。再次感謝。 – NoviceCoding 2011-05-24 02:53:23

+1

這是你需要嘗試的地方。我向您展示了getName_andExtension(f)處理程序,您可以根據需要查看代碼以計算名稱。所以把它放在其他代碼中,代替其他代碼計算名稱的位置。 – regulus6633 2011-05-24 07:24:39

+0

當有人試圖學習編程時,我明白「給予幫助,讓他們自己做」的方法。這是一次我必須處理applescript(PHP人在這裏)的情況,所以它真的不值得我學習所有的語法和命令,而不是,我希望在這個上得到A-Z的幫助。不過謝謝,upvoted和檢查你的答案! – NoviceCoding 2011-05-25 15:49:12

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中選擇第一項修改後的名稱,而不做任何事的。

+0

嘿,永遠不會寫一個蘋果腳本,但我如何將它整合到代碼? http://pastebin.com/GHaQCcex – NoviceCoding 2011-05-23 18:34:03

+0

還有無論如何處理拖到腳本上的多個文件? – NoviceCoding 2011-05-23 18:34:17

+0

@NoviceCoding - 您可以通過在AppleScript Editor中打開現有腳本並使用建議的代碼來更改新文件名稱的定義方式來將其集成。 – 2011-05-24 05:11:15