2016-12-03 70 views
0

使用JavaScript进行自动化时,发现自己无法在AppleScript中执行非常简单的操作。 (令人震惊的,我知道。)使用JavaScript for Automation取消选择Finder中的全部

这个的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的塞拉利昂,脚本编辑器2.9)

回答

0

[编辑]这里的另一种 “变通”,你可能会考虑更好。 这将通过osascript脚本的AppleScript的版本,称为经由JXA(把戏当然是拿到转义字符右)做外壳:

var app = Application.currentApplication(); 
app.includeStandardAdditions = true; 

app.doShellScript('osascript -e "tell application \\"Finder\\" to set selection to {}"'); 

------------原来的回答-------------

好吧,我很确定我应该为这个答案道歉,但是,好吧,它确实工作,通过一个奇怪的解决办法,因为我可以在玩了几个小时后没有得到更优雅的解决方案。谁知道,也许你会发现这个优雅。我几乎。或者,也许有人会用更好的一个加入。另外,我只在脚本编辑器中一直搞这个。 [编辑]哦,还有,我还在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); 
+0

是的,你的第二个解决方案就是我最终的目标。像这样的混合可能不是最优雅的,但它比关闭和重新打开窗户更好。谢谢! –

相关问题