Applescript 文件命名

发布于 2024-11-08 20:13:21 字数 263 浏览 0 评论 0原文

代码片段:

 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

它将文件另存为 filenames.pngs 我希望它另存为 filenames.png。有什么想法如何解决这个问题吗?

Code snippet:

 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

段念尘 2024-11-15 20:13:21

将其保存为应用程序,然后您可以将内容放在上面,它会复制它们并在名称中添加 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

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
后来的我们 2024-11-15 20:13:21

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 选择中的第一个项目构建修改后的名称,而不对其进行任何操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文