2009-12-08 37 views
5

我試圖根據鼠標光標的位置來獲取兩個顯示器的當前顯示分辨​​率。AppleScript如何獲取當前顯示分辨​​率?

即當鼠標光標在第一個顯示器上時,我想獲得該顯示器的分辨率。

有了一個shell腳本,我可以得到兩項決議:

set screenWidth to (do shell script "system_profiler SPDisplaysDataType | grep Resolution | awk '{print $2}'") 

但我沒有得到這顯示當前「活動」。

任何想法?

回答

8

即使通過系統事件,Applescript也不能訪問光標位置。抱歉。

[有幾個商業解決方案,但我猜他們不值得在這種情況下麻煩?我想我也可以掀起一個快速的命令行工具,只是返回當前的光標位置...值得的麻煩?]

p.s. AWK是在尋找匹配的行很大:

set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'") 
4

爲了完整起見,這裏是抓取屏幕高度的代碼:

do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $4}'"} 
6

該做的伎倆:

tell application "Finder" 
set screen_resolution to bounds of window of desktop 
end tell 
+2

,只有擁有一個顯示效果雖然。 – user495470 2011-10-30 11:22:04

+0

對於多個顯示器,「桌面窗口邊界」的邊界根據其在系統偏好設置中定義的空間佈置報告*單個組合*大小,該大小是圍繞所有顯示器*的包圍矩形。 換句話說:你不能分辨出有多少個顯示,並且報告的矩形可能包含實際上不可顯示的區域。 類似地,標準套件窗口對象(AppleScriptable應用程序的窗口,通過'bounds')和Process Suite'窗口'對象(通過'position'的上下文'「System Events」')報告它們的座標組合矩形。 – mklement0 2013-10-25 15:42:16

3

以下內容不能解決OP的問題,但對於想要確定AppleScript中所有附加顯示器的分辨率(感謝@JoelReid和@ iloveitaly用於積木):

set resolutions to {} 
repeat with p in paragraphs of ¬ 
    (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution:/{ printf \"%s %s\\n\", $2, $4 }'") 
    set resolutions to resolutions & {{word 1 of p as number, word 2 of p as number}} 
end repeat 
# `resolutions` now contains a list of size lists; 
# e.g., with 2 displays, something like {{2560, 1440}, {1920, 1200}} 
+0

這太好了,你能想出一個辦法或知道哪個是活動桌面?我正在考慮尋找一種方法來了解桌面是否處於活動窗口 – perrohunter 2014-10-15 19:30:16

+1

@perrohunter我不知道如何在AppleScript中單獨使用這種方法。雖然您可以按照以下方式獲取最前面的應用程序活動窗口的界限:「第一個應用程序窗口的邊界(作爲文本的最前面的應用程序的路徑)」,這些邊界用包含_all_顯示的虛擬矩形報告,在「Finder」上下文中,桌面窗口的邊界。如果不知道你的顯示器是如何佈置的(垂直,水平,向左,向右,......),你將無法推斷出具體的顯示。 – mklement0 2014-10-15 20:31:51

5

對於更完整起見,這裏是代碼得到的寬度,高度,和一個特定顯示器(主或內置)的視網膜規模。

這是代碼即可獲得內置顯示的高解析規模:

set {width, height, scale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Built-In: Yes/{found=1} /Resolution/{width=$2; height=$4} /Retina/{scale=($2 == \"Yes\" ? 2 : 1)} /^ {8}[^ ]+/{if(found) {exit}; scale=1} END{printf \"%d %d %d\\n\", width, height, scale}'") 

這是代碼來獲取主顯示器的分辨率和視網膜規模:

set {width, height, scale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Main Display: Yes/{found=1} /Resolution/{width=$2; height=$4} /Retina/{scale=($2 == \"Yes\" ? 2 : 1)} /^ {8}[^ ]+/{if(found) {exit}; scale=1} END{printf \"%d %d %d\\n\", width, height, scale}'") 

的代碼是基於this post由傑西鮑曼這裏給出的答案。

-1

在我的機器system_profiler需要近一秒鐘才能返回答覆。對我而言,這種方式太長了。我使用ASObjC Runner,但顯然不再有效。

這是快得多我:

tell application "Finder" to get bounds of window of desktop

(來自https://superuser.com/a/735330/64606兩者)​​

0

要獲得寬度,高度和縮放(視網膜= 2,否則= 1)所有顯示器:

set resolutions to {} 
repeat with p in paragraphs of ¬ 
    (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution:/{ printf \"%s %s %s\\n\", $2, $4, ($5 == \"Retina\" ? 2 : 1) }'") 
    set resolutions to resolutions & {{word 1 of p as number, word 2 of p as number, word 3 of p as number}} 
end repeat 

get resolutions 

基於answersabove

結果是這樣的:

{{2304, 1440, 2}, {1920, 1080, 1}}