如何使用JXA关闭所有Finder窗口

时间:2016-02-09 07:04:53

标签: macos automation applescript jxa

我基本上想将此代码从AS移植到JXA:

tell application "Finder"
    close every window
    open folder "Projects" of folder "Documents" of home
    tell the front Finder window
        set the bounds to {0, 0, 1920, 1080}
        set the current view to list view
    end tell
end tell

提前致谢!关于JXA的信息很少!

2 个答案:

答案 0 :(得分:3)

要关闭所有Finder窗口,脚本需要循环

-webkit-padding-start: 40px;

finder.finderWindows().forEach(function(w) {w.close()})

或使用地图方法:

var allWindows = finder.finderWindows()
for (var i in allWindows) {allWindows[i].close()}

答案 1 :(得分:0)

以下工作:

Application('Finder').windows.close()

唉,JXA是由Lame和Fail†组成的,所以当你运行它时会抛出一个错误,所以你必须使用一个循环来逐个关闭每个窗口。

在迭代对象说明符数组时,您需要小心,因为只有-ID说明符才能保证稳定。 (请记住:对象说明符是第一类查询,而不是指针,因此与OO样式引用的行为非常不同。)在这种情况下,jackjr的finder.finderWindows().forEach(function(w) {w.close()})将完成这项工作,因为finder.finderWindows()返回by-ID说明符的数组。但是,如果数组包含by-index说明符,那么您必须从最后到第一个迭代这些说明符,否则您将得到N个错误。

†(TBH,对于任何非平凡的自动化工作,你最好坚持使用AppleScript。语言本身可能是垃圾,但它是目前唯一支持苹果事件的选项。 )