简体   繁体   中英

Setting Finder label via AppleScript on High Sierra

I'm trying to use AppleScript to set the label of a file to a specific colour; or rather, I already have a script that used to work, but ever since upgrading to High Sierra it no longer does.

I've stripped it down to the absolute basics (always sets label to green):

on run theArguments
    set theFile to POSIX file (item 1 of theArguments)
    tell application "Finder" to set (theFile's label index) to 6
end run

If you save this to a file ( green.scpt ) then you can run it from Terminal with the following command:

osascript ~/Downloads/green.scpt ~/Downloads/green.scpt

(substitute the paths for wherever you store the script)

This should turn the script's label in the Finder to green, but doesn't (at least on High Sierra), instead giving the following error message:

/Users/haravikk/Downloads/green.scpt: execution error: Finder got an error: Can’t set label index of file "Users:haravikk:Downloads:green.scpt" to 6. (-10006)

Am I doing something wrong here? If not, and this is a bug, then is there some other way to change a file's label via script?

You can try to use tell application before change label index

on run theArguments
tell application "Finder"
    set thisItem to POSIX file theArguments as alias
    if label index of thisItem is not 6 then
        set the label index of thisItem to 6
    end if
end tell
end run

This works for me (added "as alias"):

on run theArguments
    set theFile to POSIX file (item 1 of theArguments) as alias
    tell application "Finder" to set (theFile's label index) to 6
end run

Note though that labels are long gone, and have been replaced with tags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM