【问题标题】:Applescript - ghosted result with applescript set file labelApplescript - 带有 applescript 设置文件标签的幻影结果
【发布时间】:2019-10-11 06:21:37
【问题描述】:

我确实在 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

它给了我以下结果。一个幻影或仅可见的禁用文件。

我做错了什么?仅当我设置标签时才会发生这种情况。关于这一行

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

有什么建议吗?谢谢。

【问题讨论】:

  • 我猜应该停止脚本,但脚本已经结束,文件仍然不可选择。以及为什么是 -1,有人可以解释一下。
  • 猜猜看,我做到了。仍然存在,但是正确的代码解决方案是什么。
  • 不,无处不在。在我的用户文件夹中,在 diff USB 驱动器上,在附加的 TB3 设备上以及在 NAS afp 上。有没有办法重新加载取景器?但还是想知道为什么是-1?
  • 奇怪,马特,你的一些反应消失了。删除了吗?
  • 当我尝试删除该文件时,我确实收到一条通知,指出有一个应用程序正在使用该文件。 - 我应该使用标签而不是标签吗?有什么建议可以让它发挥作用吗?

标签: applescript macos-mojave macos-catalina


【解决方案1】:

仍然不明白为什么文件被禁用。无论如何,继续前进,经过一些阅读后,我把它作为一个测试脚本来了,它可以工作。需要一些错误管理,例如检查现有文件。

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吗?只是好奇。 谢谢。

【讨论】:

  • 奇怪。看起来扩展属性已损坏或某些东西,因为只是创建文件并尝试使用Get Info 设置标签具有相同的结果(删除FinderInfo 属性或打开/关闭文件似乎可以修复它)。不知道为什么你被否决了,除非它是因为脚本在 Script Editor 中不起作用,尽管你确实提到它是在 Automator 中创建的。
【解决方案2】:

我在黑暗中刺了一下,并认为问题可能出在创建的文件的大小上,为 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

【讨论】:

  • 在 macOS Catilina 上,脚本有时会失败,但并不总是在 my writeToFile(txtFile as alias)error "«script» doesn’t understand the “writeToFile” message." number -1708 from «script» 处失败。它还可以创建与 OP 中相同的问题,文件显示为灰色且无法访问,as in this picture
  • 还有。我之前已经向您提到过在 on error handler 中重复与 try block 中相同的 code应该有 close access theFile 并且不要尝试再次写入,这不是处理 I/O 写入错误的正确方法!
  • @user3439894 我做了一些小改动并更新了我的代码。对于两种操作系统(Mojave 和 Catalina),只要插入位置不存在“__ReadMe.txt”文件,我就运行了多个测试并且没有抛出一个错误。让我知道我的更改是否对您产生了影响。
  • 谢谢。扩展上述脚本的一个附带问题是有一种方法可以检查 Applescript 以检查文件是否实际打开,例如 lsof?
  • 我建议(实际上,我一直在宣扬这一点)对任何人或每个人都使用系统事件处理尽可能多的文件操作。它处理 POSIX 路径,波浪号,在创建一个已经存在的文件时不会出错,并且可能没有听起来像 Finder 慌乱的重影。 Finder 仅用于插入位置和标签索引。
猜你喜欢
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多