"标记并移动" Applescript不再适用(High Sierra)

时间:2018-05-08 15:48:51

标签: macos applescript macos-high-sierra

首先,请允许我强调我几乎没有Applescript经验。我是一名甲骨文开发人员,所以请假设我的知识是零,所以如果你真的愚蠢地回应你的反应,我将感激不尽。

我大约一年前写过这个剧本,它突然停止了工作。我猜测macOS升级(在High Sierra上)不再容忍我写得很糟糕的脚本。

on run {input, parameters}

repeat with theFile in input

    set FileName to theFile as text
    tell application "Finder"
        if FileName ends with "-xs.jpg" then
            --label value, 0 is no label, 1 Orange, 2 Red, 6 Geen
            set the label index of file FileName to 6
            move FileName to folder "Macintosh HD:Users:Karla:Pictures:FTP:basket-[xs]:" with replacing

        else if FileName ends with "-sm.jpg" then
            set the label index of file FileName to 1
            move FileName to folder "Macintosh HD:Users:Karla:Pictures:FTP:thumbnails-[sm]:" with replacing

        else if FileName ends with "-lg.jpg" then
            set the label index of file FileName to 2
            move FileName to folder "Macintosh HD:Users:Karla:Pictures:FTP:main-[lg]:" with replacing

        else if FileName ends with "-xlg.jpg" then
            set the label index of file FileName to 5
            move FileName to folder "Macintosh HD:Users:Karla:Pictures:FTP:large-[xlg]:" with replacing

        end if
    end tell
end repeat
return input
end run

我得到的错误是"操作"运行AppleScript"遇到错误:" Finder收到错误:无法设置文件" Macintosh HD:用户:Karla:图片:1-IMAGES-TO-DO:image1-lg.jpg"到2。"

如果我注释掉标记并尝试移动,我会得到"动作"运行AppleScript"遇到错误:" Finder收到错误:无法获取文件夹" Mackintosh HD:用户:Karla:图片:FTP:main- [lg]:"。"

编辑:

我也尝试过:

on run {input, parameters}
    repeat with the_input_file in input
        set FileName to POSIX file (the_input_file as alias)
    end repeat    
    return input
 end run

但是得到错误动作"运行AppleScript"遇到错误:"无法获得POSIX文件(别名" Macintosh HD:用户:Karla:图片:file.jpg")。"

再次编辑:

所以我就是这样。我按如下方式创建了一个Droplet:

on open theDroppedItems
    set lg_folder to POSIX path of "Macintosh HD:Users:Karla:Pictures:test1:"
    repeat with current_item_cnt from 1 to count of theDroppedItems

    -- load the next file in the_file
    set the_current_file to item current_item_cnt of theDroppedItems
    move file the_current_file to lg_folder
end repeat
end open

我的文件仍然出现-1728错误。 "无法获取文件(别名\" Macintosh HD:用户:Karla:图片:test1:test-lg.jpg \")。"编号为-1728(别名" Macintosh HD:用户:Karla:图片:test1:test-lg.jpg")

我花了两天时间在一个曾经工作的剧本上,现在莫名其妙地没有尝试做我真正的工作。这应该会让我的生活更轻松。

2 个答案:

答案 0 :(得分:1)

尝试将此代码用于您的Droplet ...

property folderXS : "Macintosh HD:Users:Karla:Pictures:FTP:basket-[xs]"
property folderSM : "Macintosh HD:Users:Karla:Pictures:FTP:thumbnails-[sm]"
property folderLG : "Macintosh HD:Users:Karla:Pictures:FTP:main-[lg]"
property folderXL : "Macintosh HD:Users:Karla:Pictures:FTP:large-[xlg]"

on run
    --  Executed when the script is launched

    set theFiles to choose file with prompt ¬
        "Choose Files" multiple selections allowed true ¬
        without showing package contents

    repeat with theFile in theFiles
        set FileName to theFile as text
        tell application "Finder"
            if FileName ends with "-xs.jpg" then
                --label value, 0 is no label, 1 Orange, 2 Red, 6 Green
                set the label index of file FileName to 6
                move FileName to folder folderXS with replacing
            else if FileName ends with "-sm.jpg" then
                set the label index of file FileName to 1
                move FileName to folder folderSM with replacing
            else if FileName ends with "-lg.jpg" then
                set the label index of file FileName to 2
                move FileName to folder folderLG with replacing
            else if FileName ends with "-xlg.jpg" then
                set the label index of file FileName to 5
                move FileName to folder folderXL with replacing
            end if
        end tell
    end repeat
    return theFiles
end run


on open theDroppedItems
--  Executed when files are dropped on the script

    set lg_folder to "Macintosh HD:Users:Karla:Pictures:test1"
    repeat with current_item_cnt from 1 to count of theDroppedItems
        set the_current_file to item current_item_cnt of theDroppedItems
        tell application "Finder"
            move file the_current_file to lg_folder
        end tell
    end repeat
end open

答案 1 :(得分:0)

如果有人像我一样来到这里寻找答案,我可以给你一个不雅的、hacky 的方式来至少让这个工作。我有一个变量 this_item,其中包含一个我想移入 todaysFolder 的 HFS 文件。

部分问题是 Finder 并不总是知道如何处理涉及变量的 POSIX 路径,有时您需要先使用“my”关键字。其他时候,你需要把它放在外面。我无法弄清楚逻辑。

以下荒谬的代码块可靠地完成了它,并且似乎捕获了每一个奇怪的、高深莫测的异常。

         try
                
                move my (POSIX file (POSIX path of this_item)) to folder (todaysFolder) of disk "Hard Drive" with replacing
            on error
                try
                    move (POSIX file (POSIX path of this_item)) to folder (todaysFolder)  of disk "Hard Drive" with replacing
                on error
                    try
                        move (file (POSIX path of this_item)) to folder (todaysFolder) of disk "Hard Drive" with replacing
                    on error
                        move (file (this_item)) to folder (todaysFolder) of disk "Hard Drive" with replacing
                    end try
                end try
                
            end try