Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Applescript文件命名_Applescript_Filenames - Fatal编程技术网

Applescript文件命名

Applescript文件命名,applescript,filenames,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我希望它另存为filename.png。您知道如何解决此问题吗?此项的名称给出查找程序项的全名,包括文件扩展名。你需要把它分开。不幸的是

代码段:

 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我希望它另存为filename.png。您知道如何解决此问题吗?

此项的名称
给出查找程序项的全名,包括文件扩展名。你需要把它分开。不幸的是,这并不像可能的那么容易。你可以这样做:

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选择中的第一个项目构建修改后的名称,而不做任何处理

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

注意处理程序getName\u和extension(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

嘿,从来没有写过苹果的脚本,但我如何将其集成到代码中?是否仍要处理拖到脚本上的多个文件?@nearyording-通过在AppleScript编辑器中打开现有脚本并使用建议的代码更改新文件名称的定义方式来集成它。这有很大帮助。我希望我没有要求太多,但无论如何,你可以把它和这个结合起来:?我想拖动多个(不包括在我所包括的调整大小脚本中)图像到它上面,并在调整大小后将s(表示小)名称添加到它。再次非常感谢。这是你需要尝试的地方。我向您展示了getName_和extension(f)处理程序,您可以看到根据自己的喜好计算名称的代码。因此,只要把它放在其他代码中,而不是其他代码计算名称的地方。我理解当有人试图学习编程时“给予帮助,让他们自己做”的方法。这是一个一次性的情况,我将不得不处理applescript(这里是PHP人员),所以它真的不值得我学习所有的语法和命令,还有什么,我希望在这方面得到a-Z帮助。尽管如此,还是要谢谢你,投票并检查你的答案!