2011-05-23 72 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