Issues with “On Open” Applescript handler in Yosemite

百般思念 提交于 2019-12-12 13:01:55

问题


In using Applescript in OSX 10.10 (Yosemite), it seems Apple has changed some of the default behavior.

on open dropped_files
   display dialog (count of dropped_files)
end open

This very basic Applescript highlights the problem. If I select a group of 6 files from the Finder, and drop/drop onto a compiled version of this script, I get the response "2" and then the response "4". It should be responding "6"... but it's almost as if Finder is parsing the files into smaller groups. If I do this again, I get a different combination of numbers, so it does not seem to be consistent.

This is not the desired behavior for my application, any ideas for a solution? I never saw this behavior with older versions of Applescript.


回答1:


This bizarre effect is due to quarantined files. Quarantined files can be checked with the command:

xattr -p com.apple.quarantine *

Depending on the sort order of the quarantined/non-quarantined files, it will separately execute the "on open" handler for each group (be it quarantined or non-quarantined): e.g., 1 - quarantined, 4 - non-quarantined, 3 - quarantined. You'll notice there are two groups of quarantined files being submitted in this example, and that's because of how that particular list was sorted and submitted to the on open handler.

This behavior is rather surprising, and I've submitted it as a bug report to Apple. The quarantine attribute can be removed with this command:

sudo xattr -dr com.apple.quarantine *

to show the correct number of files. Also, see the Applescript trick above by regulus6633 for a clever workaround.




回答2:


Wow, that's strange. This workaround seems to work. We can take advantage of properties and the "on quit" handler which is automatically run after the "on open" handler. So just run your normal code in the "on quit" handler using dFiles. Remember to set dFiles to {} at the very end else dFiles will not work properly the next time you drop files.

property dFiles : {}

on open dropped_files
    set dFiles to dFiles & dropped_files
end open

on quit
    display dialog (count of dFiles)
    set dFiles to {}
    continue quit
end quit


来源:https://stackoverflow.com/questions/28827249/issues-with-on-open-applescript-handler-in-yosemite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!