下面的applescript 将AppUninstaller.scpt 注册为垃圾文件夹的文件夹操作。
在 Mac OSX 10.7 和 10.8 中注册文件夹操作脚本工作正常。

在 10.9 中,我收到错误“ Attach error System Events got an error: Can't make alias “Macintosh HD:Users:[username]:.Trash:” into type specifier。

执行此语句时发生错误

 attach action to _trashFolder using _uninstallerScriptPath

完整的脚本如下。
on run
    tell utils

        init()
        registerFolderAction()

    end tell
end run


script utils
      property _uninstallerScript : "AppUninstaller.scpt"
      property _resRelativePath : ":Applications:TestDemo.app:Contents:Resources:"
      property _folderActionScriptRelativePath : "Scripts:Folder Action Scripts"

      global _resPath
      global _trashFolder
      global _uninstallerScriptPath

      on init()
                  -- Setup paths
                set _trashFolder to path to trash folder

                set _uninstallerScriptPath to getUninstallerScript()

                  -- Add boot disk name to App relative path
                tell application "Finder"
                          set startupDisk to (name of startup disk)
                          set _resPath to startupDisk & _resRelativePath
                end tell

                set scriptFolderPath to getScriptPath()

             -- Copy folder action script file from appbundle to scripts folder
             copyScript()

      end init

      on registerFolderAction()
                try
                     tell application "System Events"
                          set folder actions enabled to true
                          log _uninstallerScriptPath

                          -- problem with below statement.
                          attach action to _trashFolder using _uninstallerScriptPath

                          end tell
                on error msg
                          display dialog "Attach error " & msg
                end try

      end registerFolderAction

      on getScriptPath()
                return ((path to library folder from user domain) as string) & _folderActionScriptRelativePath
      end getScriptPath

      on getUninstallerScript()
                return getScriptPath() & ":" & _uninstallerScript
      end getUninstallerScript

      -- copying the script inside app bundle into scripts folder.
      on copyScript()
                tell application "Finder"
                          set srcFile to _resPath & _uninstallerScript
                          set dstFile to my getScriptPath()

                          log "Src File " & srcFile & " dstFolder " & dstFile
                          duplicate file srcFile to dstFile with replacing
                end tell
      end copyScript

end script

最佳答案

查看错误(并在我自己测试之后)似乎在小牛之前的系统中,当参数是别名时,attach action to 命令的第一个参数被正确强制转换为文件/文件夹对象说明符。在小牛队中,这种强制不会发生,并且会发生错误,因为给定的参数不是对象/类型说明符而是别名类。 attach action to 的第一个参数需要是一个对象/类型说明符,以便您可以通过在调用命令时强制强制转换来解决您的问题。

attach action to folder (_trashFolder as text) using _uninstallerScriptPath

你可以用参数 using 做同样的事情

关于macos - applescript (10.9) 不能将别名变成类型说明符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22628173/

10-12 17:26