Applescript - призрачный результат с меткой файла набора appleScript - PullRequest
0 голосов
/ 11 октября 2019

Я сделал этот скрипт в Automator.

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 голосов
/ 12 октября 2019

Я сделал удар в темноте и подумал, что, возможно, проблема в том, что размер файла, который был создан, был 0 КБ. Я решил добавить обработчик с помощью команды 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
0 голосов
/ 11 октября 2019

Все еще не понимаю, почему файл отключен. Во всяком случае и двигаться вперед, после некоторого чтения я пришел с этим в качестве тестового сценария, и он работает. Нужно исправить ошибки, например проверить существующий файл.

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? просто любопытно. Спасибо.

...