簡體   English   中英

如何使用JXA關閉所有Finder窗口

[英]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窗口,腳本需要循環

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'

這種單行代碼將關閉每個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'

以下應該工作:

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

J,JXA由Lame and Fail制成,因此在運行它時會拋出錯誤,因此必須使用循環來一次關閉每個窗口。

但是,在遍歷對象說明符數組時,您確實需要小心,因為只能保證by-ID說明符是穩定的。 (請記住:對象說明符是一流的查詢,而不是指針,因此其行為與OO風格的引用非常不同。)在這種情況下,jackjr的finder.finderWindows().forEach(function(w) {w.close()})將完成這項工作,因為finder.finderWindows()返回一個由by-ID指定符組成的數組。 但是,如果數組包含按索引指定符,那么您必須從最后到第一個對這些指定符進行迭代,否則您將得到N個錯誤。

†(TBH,對於任何不重要的自動化工作,您最好堅持使用AppleScript。該語言本身可能有點廢話,但這是當前唯一支持的選項,它實際上可以正確地說明Apple事件。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM