AppleScript-具有AppleScript设置文件标签的重影结果

时间:2019-10-11 06:21:37

标签: applescript macos-mojave macos-catalina

我确实在自动化器中制作了这个脚本。

property unset : 0
property orange : 1
property red : 2
property yellow : 3
property blue : 4
property purple : 5
property green : 6
property grey : 7

property tmplFileName : "__ReadMe" as string
property fileType : ".txt" as string
property labelTag : red

on run {input, parameters}

tell application "Finder"

    set currentPath to insertion location as text
    -- set filePath to POSIX path of currentPath
    set txtfilename to tmplFileName & fileType
    set txtFile to make new file at insertion location as alias with properties {name:txtfilename}
    set the label index of the item (txtFile as alias) to my labelTag

    select txtFile
    open txtFile

end tell

return input
end run

给我以下结果。重影或仅可见的禁用文件。

the textfile is ghosted.

我怎么了?仅当我设置标签时才会发生这种情况。关于这条线

set the label index of the item (txtFile as alias) to my labelTag

有什么建议吗?谢谢。

2 个答案:

答案 0 :(得分:0)

仍然不理解为什么文件被禁用。无论如何,并且继续前进,经过一番阅读之后,我将其作为测试脚本来使用,并且可以正常工作。需要一些错误管理,例如检查现有文件。

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property |NSURL| : a reference to current application's NSURL

property tmplFileName : "__ReadMe" as string
property fileType : ".txt" as string
property labelTag : "red"

-- Replace tags; 
--      setTags - pass a list with new tags and replacing any existing
--      forPath - POSIX path

on setTags:tagList forPath:POSIXPath 
    set aURL to current application's |NSURL|'s fileURLWithPath:POSIXPath
    aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:

on run {input, parameters}

  tell application "Finder"

    set txtfilename to tmplFileName & fileType
    set txtFile to make new file at insertion location as alias with properties {name:txtfilename}
    set POSIXPath to POSIX path of (txtFile as text)
    (my setTags:{labelTag} forPath:POSIXPath)
    select txtFile

    tell application "TextEdit"
        activate
        open txtFile as alias
    end tell

  end tell

  return input
end run

希望这对其他人也有帮助。我现在是否能从上方获得一票通过。作为附带说明,有人可以解释为什么我得到-1的原因吗?只是好奇。 谢谢。

答案 1 :(得分:0)

我在黑暗中刺了一下,以为问题可能出在创建的文件大小为0 kb。我决定使用write to file命令添加一个处理程序,并在文件中添加一个空格作为字符。这使文件大小为1个字节,然后设置标签索引有效。出于我的目的,我对您的初始代码进行了一些小的调整,但这是我所做的更改和我的代码版本。

local unset, orange, red, yellow, blue, purple, green, grey

set {unset, orange, red, yellow, blue, purple, green, grey} to {0, 1, 2, 3, 4, 5, 6, 7}

set tmplFileName to "__ReadMe"
set nameExtension to ".txt"
set labelTag to red

tell application "Finder"
    set currentPath to insertion location
    set txtfilename to tmplFileName & nameExtension
    set txtFile to make new file at insertion location as alias with properties {name:txtfilename, name extension:nameExtension}

    my writeToTheFile(txtFile as alias)

    set the label index of (txtFile as alias) to labelTag

    open txtFile
end tell

on writeToTheFile(txtFile as alias)
    set theFile to txtFile as alias
    set theText to " "
    try
        set writeToFile to open for access theFile with write permission
        write theText to writeToFile as text starting at eof
        close access theFile
    on error errMsg number errNum
        try
            close access theFile
        end try
    end try
end writeToTheFile