AppleScript ObjC

[AppleScript] スクリプトに引数を渡す


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

#!/usr/bin/env osascript
###↑必須

############################
### 各種共通宣言
############################
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application

############################
### argument 引数の受け取り処理
############################
on run {arg1, arg2}
  
  ###次の処理に渡す値をリストにして
  set listArg to {arg1, arg2}
  
  ###次の処理に渡す
open listArg
end run


############################
###本処理 前処理から値を受取
############################
on open listArg
  
  ### 処理をここに
  
end open




|

The Objective-C Programming Language

ダウンロード - The Objective-C Programming Language







|

[Framework]主要なフレームワーク

Accounts.framework
AddressBook.framework
AppKit.framework
AppleScriptKit.framework
AppleScriptObjC.framework
ApplicationServices.framework
AudioUnit.framework
AudioVideoBridging.framework
Automator.framework
AVFoundation.framework
AVKit.framework
CalendarStore.framework
CloudKit.framework
Collaboration.framework
Contacts.framework
ContactsUI.framework
CoreAudio.framework
CoreAudioKit.framework
CoreBluetooth.framework
CoreData.framework
CoreImage.framework
CoreLocation.framework
CoreMediaIO.framework
CoreMIDI.framework
CoreTelephony.framework
CoreWLAN.framework
CryptoTokenKit.framework
DiscRecording.framework
DiscRecordingUI.framework
EventKit.framework
ExceptionHandling.framework
FinderSync.framework
Foundation.framework
GameController.framework
GameKit.framework
GameplayKit.framework
GLKit.framework
ImageCaptureCore.framework
IMServicePlugIn.framework
InputMethodKit.framework
InstallerPlugins.framework
InstantMessage.framework
IOBluetooth.framework
IOBluetoothUI.framework
IOKit.framework
JavaFrameEmbedding.framework
JavaScriptCore.framework
JavaVM.framework
Kernel.framework
LocalAuthentication.framework
MapKit.framework
MediaLibrary.framework
Metal.framework
MetalKit.framework
ModelIO.framework
MultipeerConnectivity.framework
NetworkExtension.framework
NotificationCenter.framework
OpenDirectory.framework
OSAKit.framework
Photos.framework
PreferencePanes.framework
PubSub.framework
Python.framework
QTKit.framework
Quartz.framework
QuartzCore.framework
SceneKit.framework
ScreenSaver.framework
Scripting.framework
ScriptingBridge.framework
SecurityFoundation.framework
SecurityInterface.framework
Social.framework
SpriteKit.framework
StoreKit.framework
SyncServices.framework
WebKit.framework

|

[NSCFBoolean] integerValue() boolValue() intValue()

エイリアスかシンボリックリンクか
ファイルかフォルダかの判定


#!/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

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

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL
property objNSArray : a reference to objMe's NSArray
property objNSDirectoryEnumerationSkipsSubdirectoryDescendants : a reference to objMe's NSDirectoryEnumerationSkipsSubdirectoryDescendants
property objNSNotFound : a reference to 9.22337203685477E+18 + 5807
set objFileManager to objMe's NSFileManager's defaultManager()


property refNSURLIsSymbolicLinkKey : a reference to objMe's NSURLIsSymbolicLinkKey

property refNSURLNameKey : a reference to objMe's NSURLNameKey
property refNSURLPathKey : a reference to objMe's NSURLPathKey

property refNSURLIsDirectoryKey : a reference to objMe's NSURLIsDirectoryKey
property refNSURLIsRegularFileKey : a reference to objMe's NSURLIsRegularFileKey

property refNSURLIsAliasFileKey : a reference to objMe's NSURLIsAliasFileKey



set strDirPath to "/Users/XXXXXXXXXX/Desktop"

##Make NSString
set ocidNSString to objNSString's stringWithString:strDirPath
log ocidNSString as text
log ocidNSString's className() as text
log ocidNSString's stringByStandardizingPath()

set ocidURLPath to objNSURL's fileURLWithPath:ocidNSString
log ocidURLPath as text
log ocidURLPath's className() as text




set ocidFileFile to objFileManager's contentsOfDirectoryAtURL:ocidURLPath includingPropertiesForKeys:[refNSURLNameKey, refNSURLPathKey, refNSURLIsAliasFileKey, refNSURLIsSymbolicLinkKey, refNSURLIsDirectoryKey, refNSURLIsRegularFileKey] options:objNSDirectoryEnumerationSkipsSubdirectoryDescendants |error|:(missing value)


log "Repeat######"
repeat with objFiles in ocidFileFile
log objFiles as text

log "NSURLIsAliasFileKey"
log (objFiles's resourceValuesForKeys:[refNSURLIsAliasFileKey] |error|:(missing value)) as record
log NSURLIsAliasFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsAliasFileKey] |error|:(missing value))
set boolIsAliasFileKey to NSURLIsAliasFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsAliasFileKey] |error|:(missing value))
log className() of boolIsAliasFileKey as text
-->(*__NSCFBoolean*)
log "integer: " & boolIsAliasFileKey's integerValue()
log "boolean: " & boolIsAliasFileKey's boolValue()
####判定用にはこの方法で取得する
set numIsAliasFileKey to (NSURLIsAliasFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsAliasFileKey] |error|:(missing value)))'s integerValue()
set numIsAliasFileKey to (NSURLIsAliasFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsAliasFileKey] |error|:(missing value)))'s intValue()
set boolIsAliasFileKey to (NSURLIsAliasFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsAliasFileKey] |error|:(missing value)))'s boolValue()
####判定用にはこの方法で取得する
set numIsAliasFileKey to NSURLIsAliasFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsAliasFileKey] |error|:(missing value)) as integer
set boolIsAliasFileKey to NSURLIsAliasFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsAliasFileKey] |error|:(missing value)) as boolean


log "NSURLIsSymbolicLinkKey"
log (objFiles's resourceValuesForKeys:[refNSURLIsSymbolicLinkKey] |error|:(missing value)) as record
log NSURLIsSymbolicLinkKey of (objFiles's resourceValuesForKeys:[refNSURLIsSymbolicLinkKey] |error|:(missing value))
set boolIsSymbolicLinkKey to NSURLIsSymbolicLinkKey of (objFiles's resourceValuesForKeys:[refNSURLIsSymbolicLinkKey] |error|:(missing value))
log className() of boolIsSymbolicLinkKey as text
-->(*__NSCFBoolean*)
log "integer: " & boolIsSymbolicLinkKey's integerValue()
log "boolean: " & boolIsSymbolicLinkKey's boolValue()
####判定用にはこの方法で取得する
set numIsSymbolicLinkKey to (NSURLIsSymbolicLinkKey of (objFiles's resourceValuesForKeys:[refNSURLIsSymbolicLinkKey] |error|:(missing value)))'s integerValue()
set numIsSymbolicLinkKey to (NSURLIsSymbolicLinkKey of (objFiles's resourceValuesForKeys:[refNSURLIsSymbolicLinkKey] |error|:(missing value)))'s intValue()
set boolIsSymbolicLinkKey to (NSURLIsSymbolicLinkKey of (objFiles's resourceValuesForKeys:[refNSURLIsSymbolicLinkKey] |error|:(missing value)))'s boolValue()
####判定用にはこの方法で取得する
set numIsSymbolicLinkKey to NSURLIsSymbolicLinkKey of (objFiles's resourceValuesForKeys:[refNSURLIsSymbolicLinkKey] |error|:(missing value)) as integer
set boolIsSymbolicLinkKey to NSURLIsSymbolicLinkKey of (objFiles's resourceValuesForKeys:[refNSURLIsSymbolicLinkKey] |error|:(missing value)) as boolean



log "NSURLIsDirectoryKey"
log (objFiles's resourceValuesForKeys:[refNSURLIsDirectoryKey] |error|:(missing value)) as record
set boolIsDirectoryKey to NSURLIsDirectoryKey of (objFiles's resourceValuesForKeys:[refNSURLIsDirectoryKey] |error|:(missing value))
log className() of boolIsDirectoryKey
-->(*__NSCFBoolean*)
log "integer: " & boolIsDirectoryKey's integerValue()
log "boolean: " & boolIsDirectoryKey's boolValue()
####判定用にはこの方法で取得する
set numIsDirectoryKey to (NSURLIsDirectoryKey of (objFiles's resourceValuesForKeys:[refNSURLIsDirectoryKey] |error|:(missing value)))'s integerValue()
set numIsDirectoryKey to (NSURLIsDirectoryKey of (objFiles's resourceValuesForKeys:[refNSURLIsDirectoryKey] |error|:(missing value)))'s intValue()
set boolIsDirectoryKey to (NSURLIsDirectoryKey of (objFiles's resourceValuesForKeys:[refNSURLIsDirectoryKey] |error|:(missing value)))'s boolValue()
####判定用にはこの方法で取得する
set numIsDirectoryKey to NSURLIsDirectoryKey of (objFiles's resourceValuesForKeys:[refNSURLIsDirectoryKey] |error|:(missing value)) as integer
set boolIsDirectoryKey to NSURLIsDirectoryKey of (objFiles's resourceValuesForKeys:[refNSURLIsDirectoryKey] |error|:(missing value)) as boolean

log "NSURLIsRegularFileKey"
log (objFiles's resourceValuesForKeys:[refNSURLIsRegularFileKey] |error|:(missing value)) as record
set boolIsRegularFileKey to NSURLIsRegularFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsRegularFileKey] |error|:(missing value))
log className() of boolIsRegularFileKey
-->(*__NSCFBoolean*)
log "integer: " & boolIsRegularFileKey's integerValue()
log "boolean: " & boolIsRegularFileKey's boolValue()
####判定用にはこの方法で取得する
set numIsRegularFileKey to (NSURLIsRegularFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsRegularFileKey] |error|:(missing value)))'s integerValue()
set numIsRegularFileKey to (NSURLIsRegularFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsRegularFileKey] |error|:(missing value)))'s intValue()
set boolIsRegularFileKey to (NSURLIsRegularFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsRegularFileKey] |error|:(missing value)))'s boolValue()
####判定用にはこの方法で取得する
set numIsRegularFileKey to NSURLIsRegularFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsRegularFileKey] |error|:(missing value)) as integer
set boolIsRegularFileKey to NSURLIsRegularFileKey of (objFiles's resourceValuesForKeys:[refNSURLIsRegularFileKey] |error|:(missing value)) as boolean

if numIsSymbolicLinkKey = 1 then
log "シンボリックリックです"
else if numIsAliasFileKey = 1 then
log "エイリアスです"
else if numIsDirectoryKey = 1 then
log "フォルダです"
else if numIsRegularFileKey = 1 then
log "ファイルです"
end if


if boolIsSymbolicLinkKey is true then
log "シンボリックリックです"
else if boolIsAliasFileKey is true then
log "エイリアスです"
else if boolIsDirectoryKey is true then
log "フォルダです"
else if boolIsRegularFileKey is true then
log "ファイルです"
end if


log "NSURLPathKey"
####_NSURLPathKey-->アンダースコアが先頭に入ります
log (objFiles's resourceValuesForKeys:[refNSURLPathKey] |error|:(missing value)) as record
log _NSURLPathKey of (objFiles's resourceValuesForKeys:[refNSURLPathKey] |error|:(missing value)) as text
log "NSURLNameKey"
log (objFiles's resourceValuesForKeys:[refNSURLNameKey] |error|:(missing value)) as record
log NSURLNameKey of (objFiles's resourceValuesForKeys:[refNSURLNameKey] |error|:(missing value)) as text

end repeat




#########################ログ表示
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
tell application "Script Editor"
tell application "System Events"
tell process "Script Editor"
tell window 1
tell splitter group 1
tell splitter group 1
tell group 1
tell checkbox "返された値"
set boolValue to value as boolean
end tell
if boolValue is false then
click checkbox "返された値"
end if
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end if
end repeat

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

|

[NSUUID] initWithUUIDString

#!/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

######ログ表示
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 objNSNotFound : a reference to 9.22337203685477E+18



###初期化
set ocidUuidA to objMe's NSUUID's alloc()'s init()
log className() of ocidUuidA as text
log ocidUuidA
###テキスト形式で取得する
set ocidUUID to ocidUuidA's UUIDString
log className() of ocidUUID as text
log ocidUUID as text

###↑と同じ内容で別のUUIDの初期化
set ocidUuidB to objMe's NSUUID's alloc()'s initWithUUIDString:ocidUUID
log className() of ocidUuidB as text
log ocidUuidB's UUIDString as text

### 比較
set ocidCompare to ocidUuidA's compare:ocidUuidB

log class of ocidCompare
log ocidCompare



display notification "処理終了" with title "処理が終了" subtitle "処理が終了しました" sound name "Sonumi"
log ">>>>>>>>>>>>処理終了<<<<<<<<<<<<<<<"
return ">>>>>>>>>>>>処理終了<<<<<<<<<<<<<<<"




#########################ログ表示
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
#########################

|

[NSUUID] UUIDString UUIDの取得

#!/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

######ログ表示
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 objNSNotFound : a reference to 9.22337203685477E+18



###初期化
set ocidUUID to objMe's NSUUID's alloc()'s init()
log className() of ocidUUID as text
log ocidUUID
###テキスト形式で取得する
log ocidUUID's UUIDString as text



display notification "処理終了" with title "処理が終了" subtitle "処理が終了しました" sound name "Sonumi"
log ">>>>>>>>>>>>処理終了<<<<<<<<<<<<<<<"
return ">>>>>>>>>>>>処理終了<<<<<<<<<<<<<<<"




#########################ログ表示
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
#########################

|

[PDFDocument]PDFのページ数を取得する


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#
006#
007# com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009
010use AppleScript version "2.8"
011use framework "Foundation"
012use framework "PDFKit"
013use scripting additions
014
015property refMe : a reference to current application
016
017
018set aliasFile to (choose file with prompt "PDFファイルを選んでください" default location (path to desktop folder from user domain) of type {"com.adobe.pdf"} with invisibles and showing package contents without multiple selections allowed) as alias
019-->alias
020set strFilePath to POSIX path of aliasFile
021set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
022set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
023set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
024
025####ページ数を数える
026
027set ocidPDFDocument to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)
028set numPdfPageCnt to ocidPDFDocument's pageCount()
029
030log numPdfPageCnt
031numPdfPageCnt
AppleScriptで生成しました

|

[Basic] log 内容の確認

#!/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
objMe : a reference to current application
property
objNSString : a reference to objMe's NSString

###元のテキスト
set
strFilePath to "/System/Applications/Preview.app/Contents/Info.plist"

###NSStringテキスト
set
objFilePath to objNSString's stringWithString:strFilePath


log objFilePath
--->«class ocid» id «data optr00000000802A6E0200600000»

log objFilePath as text
--->/System/Applications/Preview.app/Contents/Info.plist

log objFilePath's className() as text
-->__NSCFString


|

[LINK]AppleScript ObjC

Foundation
https://developer.apple.com/documentation/foundation


AppleScript ObjC
https://developer.apple.com/library/archive/releasenotes/ScriptingAutomation/RN-AppleScriptObjC/index.html

|

その他のカテゴリー

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 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