モニターの解像度を求める
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | # 要 アクアセシビリティ 許可 |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "AppKIt" |
009 | use scripting additions |
010 | |
011 | property refMe : a reference to current application |
012 | |
013 | |
014 | ######################## |
015 | #モデル番号を取得 |
016 | set strCommandText to "/usr/sbin/sysctl -n hw.model" as text |
017 | set strHWmodelName to (do shell script strCommandText) as text |
018 | |
019 | ######################## |
020 | #リンク先を調べるWEBページのURL |
021 | set ocidComponents to refMe's NSURLComponents's alloc()'s init() |
022 | ocidComponents's setScheme:("https") |
023 | ocidComponents's setHost:("hw-model-names.services.jamfcloud.com") |
024 | ocidComponents's setPath:("/1/computerModels.xml") |
025 | set ocidURL to ocidComponents's |URL| |
026 | |
027 | |
028 | ######################## |
029 | #NSDATA |
030 | set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyXML) |
031 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference) |
032 | if (item 2 of listResponse) = (missing value) then |
033 | set ocidHTMLData to (item 1 of listResponse) |
034 | else if (item 2 of listResponse) ≠ (missing value) then |
035 | log (item 2 of listResponse)'s code() as text |
036 | log (item 2 of listResponse)'s localizedDescription() as text |
037 | return "NSDATAエラーしました" |
038 | end if |
039 | |
040 | ######################## |
041 | #NSXML |
042 | set ocidOption to (refMe's NSXMLNodePreserveAll) |
043 | set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidHTMLData) options:(ocidOption) |error| :(reference) |
044 | if (item 2 of listResponse) = (missing value) then |
045 | set ocidXMLDoc to (item 1 of listResponse) |
046 | else if (item 2 of listResponse) ≠ (missing value) then |
047 | log (item 2 of listResponse)'s code() as text |
048 | log (item 2 of listResponse)'s localizedDescription() as text |
049 | log "NSXMLDocumentエラー 警告がありました" |
050 | set ocidXMLDoc to (item 1 of listResponse) |
051 | end if |
052 | |
053 | ######################## |
054 | #ROOT |
055 | set ocidRoot to ocidXMLDoc's rootDocument() |
056 | ######################## |
057 | # |
058 | set listResponse to (ocidRoot's nodesForXPath:("//computer_model") |error| :(reference)) |
059 | if (item 2 of listResponse) = (missing value) then |
060 | set ocidComputerModelArray to (item 1 of listResponse) |
061 | else if (item 2 of listResponse) ≠ (missing value) then |
062 | log (item 2 of listResponse)'s code() as text |
063 | log (item 2 of listResponse)'s localizedDescription() as text |
064 | log "nodesForXPathエラー 警告がありました" |
065 | end if |
066 | |
067 | |
068 | ######################## |
069 | # |
070 | ##Array =エレメントの数 |
071 | set numCntArray to ocidComputerModelArray's |count|() |
072 | ##エレメントの数だけ繰り返し |
073 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
074 | set ociidItemArray to (ocidComputerModelArray's objectAtIndex:(itemNo)) |
075 | set ocidModelName to (ociidItemArray's elementsForName:("model_name"))'s firstObject() |
076 | set strModelName to (ocidModelName's stringValue()) as text |
077 | if strModelName is strHWmodelName then |
078 | set ocidDisplayName to (ociidItemArray's elementsForName:("display_name"))'s firstObject() |
079 | exit repeat |
080 | end if |
081 | end repeat |
082 | ###ここにモデル名が入る |
083 | set ocidModelName to ocidDisplayName's stringValue() |
084 | set ocidStrLength to ocidModelName's |length|() |
085 | set ocidRange to refMe's NSRange's NSMakeRange(0, ocidStrLength) |
086 | #インチ数取り出し |
087 | set ocidRegex to refMe's NSRegularExpression's regularExpressionWithPattern:("(\\d+)-inch") options:0 |error| :(missing value) |
088 | set ocidMatch to ocidRegex's firstMatchInString:ocidModelName options:0 range:(ocidRange) |
089 | set ocidMatchRange to ocidMatch's rangeAtIndex:(1) |
090 | set ocidSizeInch to ocidModelName's substringWithRange:(ocidMatchRange) |
091 | |
092 | |
093 | ################## |
094 | #モニター |
095 | set ocidScreenArray to refMe's NSScreen's screens() |
096 | set numCntScreen to (ocidScreenArray's |count|()) as integer |
097 | |
098 | repeat with itemIntNo from 0 to (numCntScreen - 1) by 1 |
099 | log "モニター番号: " & itemIntNo |
100 | set ocidItemScreen to (ocidScreenArray's objectAtIndex:(itemIntNo)) |
101 | set ocidItemScreenName to ocidItemScreen's localizedName() |
102 | set ocidItemScreenRect to ocidItemScreen's frame() |
103 | set numItemScreenW to (item 1 of (item 2 of ocidItemScreenRect)) |
104 | set numItemScreenH to (item 2 of (item 2 of ocidItemScreenRect)) |
105 | if itemIntNo = 0 then |
106 | # |
107 | set ocidW to (refMe's NSDecimalNumber's alloc()'s initWithString:(numItemScreenW as text)) |
108 | set ocidH to (refMe's NSDecimalNumber's alloc()'s initWithString:(numItemScreenH as text)) |
109 | set ocidD to (refMe's NSDecimalNumber's alloc()'s initWithString:(ocidSizeInch as text)) |
110 | set ocidWsq to (ocidW's decimalNumberByRaisingToPower:(2)) |
111 | set ocidHsq to (ocidH's decimalNumberByRaisingToPower:(2)) |
112 | set ocidWHsq to (ocidWsq's decimalNumberByAdding:(ocidHsq)) |
113 | # |
114 | set strDiag to ((ocidWHsq as real) ^ 0.5) as text |
115 | set ocidDiag to (refMe's NSDecimalNumber's alloc()'s initWithString:(strDiag)) |
116 | # |
117 | set ocidPPI to (ocidDiag's decimalNumberByDividingBy:(ocidD)) |
118 | |
119 | set ocidItemScall to ocidItemScreen's backingScaleFactor() |
120 | if ocidItemScall ≥ 2 then |
121 | log "Retinaディスプレイです" |
122 | set numPPI to ((ocidPPI as integer) * ocidItemScall) as integer |
123 | else |
124 | set numPPI to (ocidPPI as integer) as integer |
125 | end if |
126 | |
127 | |
128 | log "メインモニタの解像度:約 " & numPPI & " ppi" |
129 | end if |
130 | |
131 | |
132 | set ocidItemDepth to ocidItemScreen's depth() |
133 | log ocidItemScreenName as text |
134 | log ocidItemScreenRect as list |
135 | log numItemScreenW as integer |
136 | log numItemScreenH as integer |
137 | if ocidItemDepth is (refMe's NSWindowDepthTwentyfourBitRGB) then |
138 | log "24ビットカラー" |
139 | else if ocidItemDepth is (refMe's NSWindowDepthSixtyfourBitRGB) then |
140 | log "64ビットカラー" |
141 | else if ocidItemDepth is (refMe's NSWindowDepthOnehundredtwentyeightBitRGB) then |
142 | log "128ビットカラー" |
143 | end if |
144 | |
145 | |
146 | |
147 | end repeat |
148 | |
149 | |
150 | |
AppleScriptで生成しました |
| 固定リンク