2

我基本上想将此代码从 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 的信息太少了!

4

3 回答 3

3

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

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

或者

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

或使用map方法:

var finder = Application('Finder')
finder.finderWindows().map(function(w){w.close()})
finder.home.folders["Documents"].folders["Projects"].open()
finder.finderWindows[0].bounds = {"x":0, "y":0, "width":1920, "height":1000}
finder.finderWindows[0].currentView = 'list view'
于 2016-02-09T19:29:40.043 回答
2

这个单行将关闭每个 Finder 窗口:

with (Application('Finder')) close(windows)

您可以像这样实现完整的脚本:

f = Application('Finder')
w = f.windows.first

f.close(f.windows)
f.open(f.home.folders["Documents"].folders["Projects"])
w.bounds = {"x":0, "y":0, "width":1920, "height":1000}
w.currentView = 'list view'
于 2016-02-12T05:23:16.957 回答
0

以下应该有效:

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

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

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

†(TBH,对于任何重要的自动化工作,您最好坚持使用 AppleScript。语言本身可能很垃圾,但它是当前唯一支持的选项,它实际上可以正确表达 Apple 事件。)

于 2016-02-10T12:08:44.027 回答