AppleScript AppKit

[NSOpenPanel] openPanel

#!/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 framework "AppKit"
use scripting additions

######ログ表示
doLogView()

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSMutableString : a reference to objMe's NSMutableString

property objNSOpenPanel : a reference to objMe's NSOpenPanel

property objNSNotFound : a reference to 9.22337203685477E+18 + 5807


set objFileManager to objMe's NSFileManager's defaultManager()

set ocidUserDownloadsPath to (objFileManager's URLsForDirectory:(objMe's NSDownloadsDirectory) inDomains:(objMe's NSUserDomainMask))
log ocidUserDownloadsPath as list
log className() of ocidUserDownloadsPath as text
set ocidUserDownloadDirPath to ocidUserDownloadsPath's objectAtIndex:0
log ocidUserDownloadDirPath as text
log className() of ocidUserDownloadDirPath as text



tell objNSOpenPanel's openPanel()
setCanChooseFiles_(true)
setAllowsMultipleSelection_(true)
setCanChooseDirectories_(false)
setShowsHiddenFiles_(false)
setTreatsFilePackagesAsDirectories_(false)

setTitle_("ファイル選択")
setPrompt_("OK")
setMessage_("ファイルを選んでください")
setDirectoryURL_(ocidUserDownloadDirPath)
set ocidResult to its runModal() as integer
if ocidResult is objMe's NSFileHandlingPanelCancelButton then quit
set ocidFileResult to URLs()
end tell


#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[AppKit] hide() unhide() isHidden()

#!/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 framework "AppKit"
use scripting additions

property objMe : a reference to current application

######ログ表示
doLogView()

####UTI指定
set strUTI to "com.apple.TextEdit"

###UTI指定したアプリを格納
set ocidRunningApplicatios to objMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI

####格納されたアプリの1番を取得して
set ocidObjApp to ocidRunningApplicatios's objectAtIndex:0
(*
複数起動できるコマンド等は
リストを順に取得する事になる
*)

#### hide 隠しているしているか?調べる
log ocidObjApp's hide as boolean
#### unhide
log ocidObjApp's unhide as boolean
#### isHidden
log ocidObjApp's isHidden as boolean

## false-->起動中
## true-->起動していない(終了している)


####実際に隠す
ocidObjApp's hide

####2秒まって
delay 2

####隠すをやめる
ocidObjApp's unhide





#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[AppKit] isTerminated 終了しているか?=起動していない

#!/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 framework "AppKit"
use scripting additions

property objMe : a reference to current application

######ログ表示
doLogView()

####UTI指定
set strUTI to "com.apple.finder"

###UTI指定したアプリを格納
set ocidRunningApplicatios to objMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI

####格納されたアプリの1番を取得して
set ocidObjApp to ocidRunningApplicatios's objectAtIndex:0
(*
複数起動できるコマンド等は
リストを順に取得する事になる
*)

####終了しているか?調べる
log ocidObjApp's isTerminated as boolean

## false-->起動中
## true-->起動していない(終了している)



#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[AppLit] forceTerminate() 強制終了

強制終了
例:Finderの強制終了


#!/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 framework "AppKit"
use scripting additions

######ログ表示
doLogView()

property objMe : a reference to current application

####UTI指定
set strUTI to "com.apple.finder"

###UTI指定したアプリを格納
set ocidRunningApplicatios to objMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI
###アプリの数を数える
set numCntFinderApp to (count of ocidRunningApplicatios) as number

#####アプリの数が0より大きければ起動中なので
if numCntFinderApp > 0 then
####格納されたアプリの1番を取得して
set ocidObjApp to ocidRunningApplicatios's objectAtIndex:0
####強制終了させる
ocidObjApp's forceTerminate
end if
###2秒まって
delay 2
###起動
objMe's NSWorkspace's sharedWorkspace()'s launchAppWithBundleIdentifier:strUTI options:1 additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)
####起動出来なかった場合の再トライ
delay 2
###delay 2で起動出来なかった場合の再トライUTI指定したアプリを格納
set ocidRunningApplicatios to objMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI
###アプリの数を数える
set numCntFinderApp to (count of ocidRunningApplicatios) as number
if numCntFinderApp = 0 then
objMe's NSWorkspace's sharedWorkspace()'s launchAppWithBundleIdentifier:strUTI options:1 additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)
end if

ocidRunningApplicatios's release()

#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[AppKit] Application Information 起動中アプリの情報

#!/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 framework "AppKit"
use scripting additions

property objMe : a reference to current application


set objRunningApplicationsList to objMe's NSWorkspace's sharedWorkspace's runningApplications()

log className() of objRunningApplicationsList as text
log objRunningApplicationsList as list


repeat with objRunningApplication in objRunningApplicationsList
log className() of objRunningApplication as text
### localizedName
log objRunningApplication's localizedName as text
### bundleURL
log objRunningApplication's bundleURL as text
### bundleIdentifier
try
log objRunningApplication's bundleIdentifier as list
on error
log "このアプリケーションにはbundleIdentifierは未設定です"
end try

log objRunningApplication's executableArchitecture as text

### executableURL 実行可能なパス
log objRunningApplication's executableURL as text
### isFinishedLaunching 起動が終了しているか?
log objRunningApplication's isFinishedLaunching as boolean
### processIdentifier PID
log objRunningApplication's processIdentifier as text
### ownsMenuBar メニューに常駐しているか
log objRunningApplication's ownsMenuBar as boolean


end repeat

|

[AppKit]isFilePackage(atPath:) ファイルパッケージか?

あくまでも『ファイルパッケージ』なので
『インストールパッケージ』だとfalseを返します



#!/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 framework "AppKit"
use scripting additions

property objMe : a reference to current application



set strFilePath to "/System/Volumes/Data/macOS Install Data/UpdateBundle/AssetData/boot/EmbeddedOSFirmware.pkg"

set boolIsFilePackage to objMe's NSWorkspace's sharedWorkspace's isFilePackageAtPath:strFilePath

log class of boolIsFilePackage as text
log boolIsFilePackage as boolean



set strFilePath to "/Library/Documentation/License.lpdf"

set boolIsFilePackage to objMe's NSWorkspace's sharedWorkspace's isFilePackageAtPath:strFilePath

log class of boolIsFilePackage as text
log boolIsFilePackage as boolean

|

[AppKit] launchAppWithBundleIdentifier(アプリケーションの起動)

Finderの再起動のサンプル


#!/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 framework "AppKit"
use scripting additions

######ログ表示
doLogView()

property objMe : a reference to current application

####UTI指定
set strUTI to "com.apple.finder"

###UTI指定したアプリを格納
set ocidRunningApplicatios to objMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI
###アプリの数を数える
set numCntFinderApp to (count of ocidRunningApplicatios) as number

#####アプリの数が0より大きければ起動中なので
if numCntFinderApp > 0 then
####格納されたアプリの1番を取得して
set ocidObjApp to ocidRunningApplicatios's objectAtIndex:0
####終了させる
ocidObjApp's terminate
end if
###2秒まって
delay 2
###起動
objMe's NSWorkspace's sharedWorkspace()'s launchAppWithBundleIdentifier:strUTI options:1 additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)
####起動出来なかった場合の再トライ
delay 2
###delay 2で起動出来なかった場合の再トライUTI指定したアプリを格納
set ocidRunningApplicatios to objMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI
###アプリの数を数える
set numCntFinderApp to (count of ocidRunningApplicatios) as number
if numCntFinderApp = 0 then
objMe's NSWorkspace's sharedWorkspace()'s launchAppWithBundleIdentifier:strUTI options:1 additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)
end if

ocidRunningApplicatios's release()

#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[AppKit]ファイルを選択した状態でFinderを開く(selectFile: inFileViewerRootedAtPath:)

#!/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 framework "AppKit"
use scripting additions

property objMe : a reference to current application

set strAppPath to "/System/Library/CoreServices/Applications/Directory Utility.app"

set boolSelectFileResults to objMe's NSWorkspace's sharedWorkspace()'s selectFile:strAppPath inFileViewerRootedAtPath:"/"

|

[AppKit]アプリケーションを隠す(hideOtherApplications)

#!/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 framework "AppKit"
use scripting additions

property objMe : a reference to current application


set boolhideResults to objMe's NSWorkspace's sharedWorkspace()'s hideOtherApplications()

|

[AppKit]アプリケーションを指定してファイルを開く

#!/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 framework "AppKit"
use scripting additions

property objMe : a reference to current application

set strFilePath to "/Library/Documentation/Warranty.rtf" as text

set boolFileOpenResults to objMe's NSWorkspace's sharedWorkspace's openFile:strFilePath withApplication:"TextEdit"

|

より以前の記事一覧

その他のカテゴリー

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