AppleScript:从文件名创建新文件夹并将文件移动到该文件夹​​中

编程入门 行业动态 更新时间:2024-10-25 14:25:45
本文介绍了AppleScript:从文件名创建新文件夹并将文件移动到该文件夹​​中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

基本上,我正在尝试在 Automator 中创建文件夹操作,因此每当将文件添加到特定文件夹时,它都会创建一个与文件名(不带扩展名)匹配的子文件夹,然后将文件移动到该子文件夹中.

Basically, I'm trying to create a folder action in Automator so whenever a file is added to a specific folder, it will create a subfolder that matches the filename (without the extension), then move the file into that subfolder.

到目前为止,我已经成功地使用了这个站点的一个帖子(在 Automator 中创建以指定文件名命名的新文件夹) 以创建将创建新文件夹的脚本.但是,我一直无法修改脚本以将原始文件移动到新文件夹中.

So far, I have successfully used a post from this site (Create new folder named with a specified file name in Automator) to create a script that will create the new folder. However, I have been unable to modify the script to move the original file into the new folder.

任何帮助将不胜感激.这是我正在使用的完整脚本以供参考:

Any help would be appreciated. Here is the full script I am working with for reference:

    on run {input, parameters} -- make new folders from base file names

    set output to {}

    repeat with anItem in the input -- step through each item in the input

        set anItem to anItem as text
        tell application "System Events" to tell disk item anItem
            set theContainer to path of container
            set {theName, theExtension} to {name, name extension}
        end tell
        if theExtension is in {missing value, ""} then
            set theExtension to ""
        else
            set theExtension to "." & theExtension
        end if
        set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part

        tell application "Finder"
            make new folder at folder theContainer with properties {name:theName}
            set end of output to result as alias
        end tell
    end repeat

    return input -- or output
end run

提前致谢!

推荐答案

将此文件夹操作添加到您的目标文件夹:

Add this folder action to your target folder:

on adding folder items to theFolder after receiving theFiles
    repeat with aFile in theFiles
        tell application "System Events" to set {Nm, Ex, pPath} to aFile's {name, name extension, POSIX path of container}
        set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
        set thePath to (pPath & "/" & BN & "/" as text)
        do shell script "mkdir -p " & quoted form of thePath
        delay 0.5
        tell application "Finder" to move aFile to POSIX file thePath
    end repeat
end adding folder items to

这篇关于AppleScript:从文件名创建新文件夹并将文件移动到该文件夹​​中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 14:38:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915741.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   并将   文件名   到该   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!