【问题标题】:Applescript error while trying to move files with hazel尝试使用榛树移动文件时出现 Applescript 错误
【发布时间】:2013-05-11 20:38:52
【问题描述】:

我有一个applescript(如下),用于清理电视节目剧集的名称,并根据该脚本将剧集移动到其正确的文件夹中。我使用 hazel 用正确的文件激活脚本。在 applescript 编辑器中,脚本看起来不错,但 Hazel 在第 12 行给了我一个错误。这是脚本:

set theSample to theFile
set a to RemoveListFromString(theSample, {"720p", "HDTV", "x264", "IMMERSE", "-", "E01"})
set b to RemoveListFromString(a, {"..."})
set c to RemoveListFromString(b, {".."})
set d to replaceString(c, "S0", "Season ")
set e to replaceString(d, ".", " ")
if e contains "Season" then
    move theFile to "/Volumes/LaCie/Midia/Series/" & e
end if
if e does not contain "Season" then
    move theFile to "/Volumes/LaCie/Midia/Filmes"
end if
on RemoveFromString(theText, CharOrString)
    -- ljr (http://applescript.bratis-lover.net/library/string/)
    local ASTID, theText, CharOrString, lst
    set ASTID to AppleScript's text item delimiters
    try
        considering case
            if theText does not contain CharOrString then ¬
                return theText
            set AppleScript's text item delimiters to CharOrString
            set lst to theText's text items
        end considering
        set AppleScript's text item delimiters to ASTID
        return lst as text
    on error eMsg number eNum
        set AppleScript's text item delimiters to ASTID
        error "Can't RemoveFromString: " & eMsg number eNum
    end try
end RemoveFromString

on RemoveListFromString(theText, listOfCharsOrStrings)
    -- ljr (http://applescript.bratis-lover.net/library/string/)
    local ASTID, theText, CharOrString, lst
    try
        script k
            property l : listOfCharsOrStrings
        end script
        set len to count k's l
        repeat with i from 1 to len
            set cur_ to k's l's item i
            set theText to my RemoveFromString(theText, cur_)
        end repeat
        return theText
    on error eMsg number eNum
        error "Can't RemoveListFromString: " & eMsg number eNum
    end try
end RemoveListFromString

-- e.g. replaceString("Hello hello", "hello", "Bye") --> "Hello Bye"

on replaceString(theText, oldString, newString)
    -- ljr (http://applescript.bratis-lover.net/library/string/)
    local ASTID, theText, oldString, newString, lst
    set ASTID to AppleScript's text item delimiters
    try
        considering case
            set AppleScript's text item delimiters to oldString
            set lst to every text item of theText
            set AppleScript's text item delimiters to newString
            set theText to lst as string
        end considering
        set AppleScript's text item delimiters to ASTID
        return theText
    on error eMsg number eNum
        set AppleScript's text item delimiters to ASTID
        error "Can't replaceString: " & eMsg number eNum
    end try
end replaceString

有什么想法吗?

编辑:

所以,现在我有了这个:

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", "…", "E02", "EVOLVE"}
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, ".", " ")
set newbase to Replace(newbase, "   ", "")
set newbase to Replace(newbase, "  ", "")
set folderLocation to "/Volumes/LaCie/Midia/Series/"
set folderName to newbase as text
tell application "Finder"
    if newbase contains "Season" then
        if not (exists folder (POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text)) then
            set p to path to "/Volumes/LaCie/Midia/Series/"
            --make new folder at p with properties {name:newbase}
            make new folder with properties {name:folderName, location:p}
        else
            move theFile to POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text
            set name of result to newbase
        end if
    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

唯一不起作用的部分是如果文件夹不存在则创建文件夹的部分......有什么想法吗?

【问题讨论】:

  • 用 Ruby 编写这个脚本会容易得多。只是说!
  • 我不懂 Ruby,所以我无法构建它...

标签: string replace applescript


【解决方案1】:

使用 shell 脚本会更容易,但请尝试以下方式:

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 是一个别名,因此您必须将其强制转换为文本
  • 您必须使用POSIX file 告诉 Finder 移动文件并将路径转换为文件对象
  • Restoring text item delimiters is not necessary据我所知

编辑:这是一个 shell 脚本版本:

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 的文件夹:

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 安装。

【讨论】:

  • Hazel 在第一行仍然给我一个错误,它说“预期“结束”但发现“打开”。有什么想法吗?
  • 由于处理程序的原因,它不能作为嵌入式脚本工作。您必须将其保存为外部脚本。
  • 劳里 +1。在 hazel 中运行 AppleScript 操作包括隐藏处理程序中的嵌入脚本。由于您不能在处理程序中包含处理程序,因此您需要将 hazel 指向要运行的外部脚本。
  • 我尝试将其设置为外部脚本,但有时它在尝试运行 applescript 时会出现错误,有时它不会出现任何错误,但没有任何反应!
  • @DanielRuhman 检查日志或尝试使用say newbase 或注释掉重命名文件或其他内容的行。我编辑了答案以添加一个 shell 脚本版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 2015-07-10
  • 1970-01-01
  • 2022-01-23
  • 2016-06-04
  • 1970-01-01
相关资源
最近更新 更多