且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尝试使用榛子移动文件时出现 Applescript 错误

更新时间:2023-02-22 17:34:01

使用 shell 脚本会更容易,但可以尝试以下操作:

It would be easier to use a shell script, but try something like this:

on hazelProcessFile(theFile)
    set text item delimiters to ":"
    set filename to last text item of (theFile as text)
    set text item delimiters to "."
    if filename contains "." then
        set base to text items 1 thru -2 of filename as text
        set extension to "." & text item -1 of filename
    else
        set base to filename
        set extension to ""
    end if
    set text item delimiters to {"720p", "HDTV", "x264", "IMMERSE", "-", "E01", "..."}
    set ti to text items of base
    set text item delimiters to ""
    set newbase to ti as text
    set newbase to replace(newbase, "S0", "Season ")
    set newbase to replace(newbase, ".", " ") & extension
    tell application "Finder"
        if newbase contains "Season" then
            move theFile to POSIX file "/Volumes/LaCie/Midia/Series/"
            set name of result to newbase
        else
            move theFile to POSIX file "/Volumes/LaCie/Midia/Filmes/"
        end if
    end tell
end hazelProcessFile

on replace(input, x, y)
    set text item delimiters to x
    set ti to text items of input
    set text item delimiters to y
    ti as text
end replace

  • 嵌入式脚本不能包含处理程序
  • 外部脚本必须使用hazelProcessFile
  • theFile 是一个别名,因此您必须将其强制为文本
  • 您必须告诉 Finder 移动文件并使用 POSIX 文件
  • 将路径转换为文件对象
  • 不需要恢复文本项分隔符据我所知
  • 这是一个shell脚本版本:

    here is a shell script version:

basename=${1##*/}
base=${basename%.*}
[[ $basename = *.* ]] && ext=".${basename##*.}" || ext=
for x in 720p HDTV x264 IMMERSE - E01 ...; do base=${base//"$x"}; done
base=${base//S0/Season}
base=${base//./ }
if [[ $base = *Season* ]]; then
    mv "$1" "/Volumes/LaCie/Midia/Series/$base$ext"
else
    mv "$1" /Volumes/LaCie/Midia/Movies/
fi

编辑 2:例如,这会将 here.comes.honey.boo.boo.s01e04.720p-killers.mkv 移动到名为 Here Comes Honey Boo Boo Season 1代码>:

Edit 2: this would for example move here.comes.honey.boo.boo.s01e04.720p-killers.mkv to a folder named Here Comes Honey Boo Boo Season 1:

d=${1##*/}
if [[ $d =~ [Ss][0-9][0-9][Ee][0-9][0-9] ]]; then
    d=$(sed -E 's/[ .][Ss]([0-9][0-9])[Ee][0-9][0-9].*/ Season \1/;s/ Season 0/ Season /;s/\./ /g' <<< "$d")
    d=$(ruby -r rubygems -e 'require "titlecase"; puts ARGV[0].titlecase' "$d")
    target="/Volumes/LaCie/Midia/Series/$d"
    mkdir -p "$target"
    mv "$1" "$target"
else
    mv "$1" "/Volumes/LaCie/Midia/Movies"
fi

titlecase gem 可以通过 sudo gem install titlecase 安装.

The titlecase gem can be installed with sudo gem install titlecase.