在 Finder 中使用 JavaScript 取消全选自动化
deselect all in Finder using JavaScript for Automation
在使用 JavaScript 实现自动化时,我发现自己无法在 AppleScript 中做一些非常简单的事情。 (令人震惊,我知道。)
这个 AppleScript:
tell application "Finder" to set selection to {}
清除 Finder 中的选择。
我就是不知道如何在 JXA 中做同样的事情。
这是我尝试过的方法:
var finder = Application("Finder")
finder.includeStandardAdditions = true
//this selects files in the front window...
finder.select( [...array of file paths...] )
//so you'd think this might work to deselect all...
finder.select( [] )
//...but it doesn't do anything
//then I tried each of these in turn...
finder.select( null )
//Error -10010: Handler can't handle objects of this class.
finder.selection = null
//Error -10010: Handler can't handle objects of this class.
finder.selection = []
//Script Editor crashes
//...but none were successful
有什么建议吗?
(macOS Sierra,脚本编辑器 2.9)
[edit] 这是另一个 "work-around" 您可能认为更好的。
这通过 do shell 通过 JXA 调用的 osascript 发送脚本的 AppleScript 版本(诀窍当然是让转义字符正确):
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.doShellScript('osascript -e "tell application \"Finder\" to set selection to {}"');
------------原答案------------
好吧,我很确定我应该为这个答案道歉,但是,好吧,它确实有效,通过一个奇怪的解决方法,因为在玩了几个小时后我无法得到更优雅的解决方案.谁知道呢,也许您会觉得这很优雅。我几乎。或者也许有人会提出更好的建议。另外,我只是在脚本编辑器中弄乱了这个。 [edit] 哦,还有,我还在 10.10.5
//this allows use of JXA version of do shell script later
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var thefinder = Application("Finder");
//Would advise using if/then here to check if there's at least one window, like:
// if (thefinder.windows.length != 0) {
//gets front window (where selection is, always)
var fWin = thefinder.windows[0];
//gets the "target", which is the folder ref
var fWinTarget = fWin.target();
//gets the url of the target
var winU = fWinTarget.url();
//makes that a path
var winUPath = Path(winU);
//closes the original finder window (ref)
fWin.close();
//opens it up again! no more selection!
app.doShellScript("open " + winU);
在使用 JavaScript 实现自动化时,我发现自己无法在 AppleScript 中做一些非常简单的事情。 (令人震惊,我知道。)
这个 AppleScript:
tell application "Finder" to set selection to {}
清除 Finder 中的选择。
我就是不知道如何在 JXA 中做同样的事情。
这是我尝试过的方法:
var finder = Application("Finder")
finder.includeStandardAdditions = true
//this selects files in the front window...
finder.select( [...array of file paths...] )
//so you'd think this might work to deselect all...
finder.select( [] )
//...but it doesn't do anything
//then I tried each of these in turn...
finder.select( null )
//Error -10010: Handler can't handle objects of this class.
finder.selection = null
//Error -10010: Handler can't handle objects of this class.
finder.selection = []
//Script Editor crashes
//...but none were successful
有什么建议吗?
(macOS Sierra,脚本编辑器 2.9)
[edit] 这是另一个 "work-around" 您可能认为更好的。 这通过 do shell 通过 JXA 调用的 osascript 发送脚本的 AppleScript 版本(诀窍当然是让转义字符正确):
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.doShellScript('osascript -e "tell application \"Finder\" to set selection to {}"');
------------原答案------------
好吧,我很确定我应该为这个答案道歉,但是,好吧,它确实有效,通过一个奇怪的解决方法,因为在玩了几个小时后我无法得到更优雅的解决方案.谁知道呢,也许您会觉得这很优雅。我几乎。或者也许有人会提出更好的建议。另外,我只是在脚本编辑器中弄乱了这个。 [edit] 哦,还有,我还在 10.10.5
//this allows use of JXA version of do shell script later
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var thefinder = Application("Finder");
//Would advise using if/then here to check if there's at least one window, like:
// if (thefinder.windows.length != 0) {
//gets front window (where selection is, always)
var fWin = thefinder.windows[0];
//gets the "target", which is the folder ref
var fWinTarget = fWin.target();
//gets the url of the target
var winU = fWinTarget.url();
//makes that a path
var winUPath = Path(winU);
//closes the original finder window (ref)
fWin.close();
//opens it up again! no more selection!
app.doShellScript("open " + winU);