NSScreen

モニターの解像度を求める


AppleScript サンプルコード

【スクリプトエディタで開く】 |

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
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKIt"
009use scripting additions
010
011property refMe : a reference to current application
012
013
014########################
015#モデル番号を取得
016set strCommandText to "/usr/sbin/sysctl -n hw.model" as text
017set strHWmodelName to (do shell script strCommandText) as text
018
019########################
020#リンク先を調べるWEBページのURL
021set ocidComponents to refMe's NSURLComponents's alloc()'s init()
022ocidComponents's setScheme:("https")
023ocidComponents's setHost:("hw-model-names.services.jamfcloud.com")
024ocidComponents's setPath:("/1/computerModels.xml")
025set ocidURL to ocidComponents's |URL|
026
027
028########################
029#NSDATA
030set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyXML)
031set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference)
032if (item 2 of listResponse) = (missing value) then
033  set ocidHTMLData to (item 1 of listResponse)
034else 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エラーしました"
038end if
039
040########################
041#NSXML
042set ocidOption to (refMe's NSXMLNodePreserveAll)
043set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidHTMLData) options:(ocidOption) |error| :(reference)
044if (item 2 of listResponse) = (missing value) then
045  set ocidXMLDoc to (item 1 of listResponse)
046else 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)
051end if
052
053########################
054#ROOT
055set ocidRoot to ocidXMLDoc's rootDocument()
056########################
057#
058set listResponse to (ocidRoot's nodesForXPath:("//computer_model") |error| :(reference))
059if (item 2 of listResponse) = (missing value) then
060  set ocidComputerModelArray to (item 1 of listResponse)
061else 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エラー 警告がありました"
065end if
066
067
068########################
069#
070##Array =エレメントの数
071set numCntArray to ocidComputerModelArray's |count|()
072##エレメントの数だけ繰り返し
073repeat 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
081end repeat
082###ここにモデル名が入る
083set ocidModelName to ocidDisplayName's stringValue()
084set ocidStrLength to ocidModelName's |length|()
085set ocidRange to refMe's NSRange's NSMakeRange(0, ocidStrLength)
086#インチ数取り出し
087set ocidRegex to refMe's NSRegularExpression's regularExpressionWithPattern:("(\\d+)-inch") options:0  |error| :(missing value)
088set ocidMatch to ocidRegex's firstMatchInString:ocidModelName options:0 range:(ocidRange)
089set ocidMatchRange to ocidMatch's rangeAtIndex:(1)
090set ocidSizeInch to ocidModelName's substringWithRange:(ocidMatchRange)
091
092
093##################
094#モニター
095set ocidScreenArray to refMe's NSScreen's screens()
096set numCntScreen to (ocidScreenArray's |count|()) as integer
097
098repeat 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 ocidItemScall2 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  
147end repeat
148
149
150
AppleScriptで生成しました

|

メニューバーの高さ(25px)


あくまでも参考にしてください

【スクリプトエディタで開く】 |

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# メニューバーの高さ
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use framework "Cocoa"
010use scripting additions
011
012property refMe : a reference to current application
013
014set appSharedApp to refMe's NSApplication's sharedApplication()
015set strAppName to appSharedApp's |name| as text
016if strAppName is "osascript" then
017  (*
018osascriptにはメニューバーが無いのでスクリーンの差分で取ろうとしたが
019数値が意図した値にならない
020set ocidMainScreen to refMe's NSScreen's mainScreen()
021set numFlameH to item 1 of (item 2 of ocidMainScreen's frame()) as integer
022set numVFlameH to item 1 of (item 2 of ocidMainScreen's visibleFrame()) as integer
023set numMenuBarH to (numFlameH - numVFlameH) as integer
024-->なぜか?34pxになる
025*)
026  set numMenuBarH to refMe's NSMenu's menuBarHeight() as integer
027else
028  appSharedApp's |activate|()
029  set ocidMenu to appSharedApp's mainMenu()
030  set numMenuBarH to ocidMenu's menuBarHeight() as integer
031end if
032
033display alert (numMenuBarH as text)
034
035return
036
AppleScriptで生成しました

|

[NSScreen]メインスクリーンとサブスクリーン


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
# 要 アクアセシビリティ 許可
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKIt"
use scripting additions
property refMe : a reference to current application

set boolHaveSubScreen to false as boolean

set ocidScreenArray to refMe's NSScreen's screens()

if (count of ocidScreenArray) > 1 then
  log "サブスクリーンがあります"
  set boolHaveSubScreen to true as boolean
  set ocidSubScreen to ocidScreenArray's lastObject()
  set ocidSubRect to ocidSubScreen's frame()
  log ocidSubRect
else if (count of ocidScreenArray) = 1 then
  log "メインスクリーンのみです"
  set boolHaveSubScreen to false as boolean
  set ocidSubRect to {{0, 0}, {0, 0}} as list
end if
###メインスクリーン
set ocidMainScreen to refMe's NSScreen's mainScreen()
set ocidMainRect to ocidMainScreen's frame()
log ocidMainRect
set numMainW to (item 1 of (item 2 of ocidMainRect)) as integer
set numMainH to (item 2 of (item 2 of ocidMainRect)) as integer
###サブスクリーン
set numSubX to (item 1 of (item 1 of ocidSubRect)) as integer
set numSubY to (item 2 of (item 1 of ocidSubRect)) as integer
set numSubW to (item 1 of (item 2 of ocidSubRect)) as integer
set numSubH to (item 2 of (item 2 of ocidSubRect)) as integer




|

[NSScreen] Retinaか?判定する


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application


set ocidScreenArray to refMe's NSScreen's screens()
set appMainScreen to refMe's NSScreen's mainScreen()
set ocidDescription to appMainScreen's deviceDescription()
set recordDeviceResolution to refMe's NSSizeFromCGSize(ocidDescription's valueForKey:"NSDeviceResolution")
set intDeviceResolution to width of recordDeviceResolution as integer
if intDeviceResolution = 144 then
  log "解像度:" & intDeviceResolution
  log "Retinaディスプレイです"
else
  
  log "解像度:" & intDeviceResolution
end if

|

[モニタ]スクリーンサイズと拡張モニタ(考え中)


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


tell application "System Events"
  set numCntDesktop to (count of every desktop) as integer
  set listDesktop to (every desktop) as list
  
  repeat with itemDesktop in listDesktop
    tell itemDesktop
      log display name as text
    end tell
  end repeat
  
end tell

tell application "Finder"
  set listScreenBounds to (bounds of window of desktop) as list
  log listScreenBounds
  
end tell


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKIt"
use framework "Cocoa"
use scripting additions

property refMe : a reference to current application

set ocidScreenArray to refMe's NSScreen's screens()
set numCntScreen to ocidScreenArray's |count|()


repeat with itemScreen in ocidScreenArray
  
  set ocidScreenName to itemScreen's localizedName
  log ocidScreenName as text
  
  set ocidScreenBounds to itemScreen's visibleFrame
  log ocidScreenBounds as list
  
  
  set ocidScreenRect to itemScreen's frame
  log ocidScreenRect as list
  
end repeat

set appMainScreen to refMe's NSScreen's mainScreen()
log appMainScreen's visibleFrame as list
log appMainScreen's frame as list

|

[NSScreen]モニターの情報

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"

use scripting additions

property refMe : a reference to current application

set ocidAllScreens to refMe's NSScreen's screens()
log "モニタ数: " & ocidAllScreens's |count|()

repeat with objScreen in ocidAllScreens
log "--------"
set ocidDescription to objScreen's deviceDescription()
log "NSDeviceResolution:" & refMe's NSStringFromSize(ocidDescription's valueForKey:"NSDeviceResolution")
log "NSDeviceSize:" & refMe's NSStringFromSize(ocidDescription's valueForKey:"NSDeviceSize")
log "NSDeviceIsScreen: " & (ocidDescription's valueForKey:"NSDeviceIsScreen")
log "NSScreenNumber: " & (ocidDescription's valueForKey:"NSScreenNumber")
log "NSDeviceColorSpaceName: " & (ocidDescription's valueForKey:"NSDeviceColorSpaceName")
log "NSDeviceBitsPerSample: " & (ocidDescription's valueForKey:"NSDeviceBitsPerSample")

log objScreen's depth()
log objScreen's frame()
log objScreen's supportedWindowDepths()
log objScreen's colorSpace()
log objScreen's localizedName()
log objScreen's visibleFrame()
log objScreen's safeAreaInsets()
log objScreen's auxiliaryTopLeftArea()
log objScreen's auxiliaryTopRightArea()

end repeat

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom