如何使用 JXA 关闭所有 Finder windows
How to close all Finder windows using 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 的信息太少了!
要关闭所有 Finder windows,脚本需要 循环:
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'
以下应该工作:
Application('Finder').windows.close()
唉,JXA 是由 Lame 和 Fail † 组成的,所以当你 运行 它时只会抛出一个错误,所以你必须使用循环来一次关闭每个 window .
不过在遍历对象说明符数组时确实需要小心,因为只有按 ID 说明符才能保证稳定。 (请记住:对象说明符是 first-class 查询,而不是指针,因此其行为与 OO 风格的引用非常不同。)在这种情况下,jackjr 的 finder.finderWindows().forEach(function(w) {w.close()})
将完成这项工作,因为 finder.finderWindows()
returns 一组按 ID 说明符。但是,如果数组包含按索引说明符,那么您必须从最后到第一个迭代这些说明符,否则您将得到 N 次错误。
†(TBH,对于任何重要的自动化工作,您最好坚持使用 AppleScript。该语言本身可能很糟糕,但它是目前唯一支持的选项,它实际上正确地讲述了 Apple 事件。)
这一行将关闭每个 Finder window:
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'
我基本上想将此代码从 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 的信息太少了!
要关闭所有 Finder windows,脚本需要 循环:
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'
以下应该工作:
Application('Finder').windows.close()
唉,JXA 是由 Lame 和 Fail † 组成的,所以当你 运行 它时只会抛出一个错误,所以你必须使用循环来一次关闭每个 window .
不过在遍历对象说明符数组时确实需要小心,因为只有按 ID 说明符才能保证稳定。 (请记住:对象说明符是 first-class 查询,而不是指针,因此其行为与 OO 风格的引用非常不同。)在这种情况下,jackjr 的 finder.finderWindows().forEach(function(w) {w.close()})
将完成这项工作,因为 finder.finderWindows()
returns 一组按 ID 说明符。但是,如果数组包含按索引说明符,那么您必须从最后到第一个迭代这些说明符,否则您将得到 N 次错误。
†(TBH,对于任何重要的自动化工作,您最好坚持使用 AppleScript。该语言本身可能很糟糕,但它是目前唯一支持的选项,它实际上正确地讲述了 Apple 事件。)
这一行将关闭每个 Finder window:
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'