AppleScript Guide

[fig 02-09]フォルダ内のアイテム

指定フォルダ内に内包される項目
例はユーザーの音楽Musicフォルダ


tell application "Finder"
tell folder (path to music folder from user domain)
set file_list to name of every item
end tell
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 scripting additions

####ログ表示
doLogView()

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
set objFileManager to objMe's NSFileManager's defaultManager()

####ミュージックフォルダ
set ocidUserMusicPath to (objFileManager's URLsForDirectory:(objMe's NSMusicDirectory) inDomains:(objMe's NSUserDomainMask))
log ocidUserMusicPath as list
log ocidUserMusicPath as text

####UNIXパス形式に変換(NSSingleObjectArraytringで確定させる)
set ocidFileRelativePath to (ocidUserMusicPath's objectAtIndex:0)'s relativePath
log ocidFileRelativePath as text
log className() of ocidFileRelativePath as text

###NSStringにして
set ocidDirPath to objNSString's stringWithString:ocidFileRelativePath
log ocidDirPath as text
log className() of ocidDirPath as text


###ディレクトリのコンテンツ一覧
set arrayFilesList to objFileManager's contentsOfDirectoryAtPath:ocidDirPath |error|:(missing value)
####
log arrayFilesList
log arrayFilesList's className() as text
log arrayFilesList as list

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

|

[fig 02-08]フォルダのプロパティの取得

#!/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 objNSURL : a reference to objMe's NSURL
set objFileManager to objMe's NSFileManager's defaultManager()

####ミュージックフォルダ
set ocidUserMusicPath to (objFileManager's URLsForDirectory:(objMe's NSMusicDirectory) inDomains:(objMe's NSUserDomainMask))
log ocidUserMusicPath as list
log ocidUserMusicPath as text

####UNIXパス形式に変換(NSSingleObjectArraytringで確定させる)
set ocidFileRelativePath to (ocidUserMusicPath's objectAtIndex:0)'s relativePath
log ocidFileRelativePath as text
log className() of ocidFileRelativePath as text

###NSStringにして
set ocidDirPath to objNSString's stringWithString:ocidFileRelativePath
log ocidDirPath as text
log className() of ocidDirPath as text

###attributesOfItemAtPath
set ocidAttributes to objFileManager's attributesOfItemAtPath:ocidDirPath |error|:(missing value)
log ocidAttributes as list
log className() of ocidDirPath as text
(*
ocidAttributes
NSFileOwnerAccountID
NSFileSystemFileNumber
NSFileExtensionHidden
NSFileSystemNumber
NSFileSize
NSFileGroupOwnerAccountID
NSFileOwnerAccountName
NSFileCreationDate
NSFilePosixPermissions
NSFileProtectionKey
NSFileType
NSFileGroupOwnerAccountName
NSFileReferenceCount
NSFileModificationDate 等が入っています
*)

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

|

[fig 02-04]テキストの結合(追加)

#!/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
####ログ表示
doLogView()
####
property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSMutableString : a reference to objMe's NSMutableString

set strTextA to "美しい日本語" as text
set strTextB to "" as text
set strTextC to "オープンタイプフォント" as text


set strTextD to (strTextA & strTextB & strTextC) as text
log strTextD as text
log class of strTextD

set strTextE to (strTextA & strTextB & "" & strTextC) as text

log strTextE as text
log class of strTextE

########################

set strTextF to "美しい日本語" as text
set strTextG to "" as text
set strTextH to "オープンタイプフォント" as text


####NSString
set ocidTextF to objNSString's stringWithString:strTextF
set ocidTextG to objNSString's stringWithString:strTextG
set ocidTextH to objNSString's stringWithString:strTextH

set ocidTextI to ocidTextF's stringByAppendingString:ocidTextG
set ocidTextI to ocidTextI's stringByAppendingString:ocidTextH

log ocidTextI as text
log className() of ocidTextI as text

set ocidTextF to objNSString's stringWithString:strTextF
set ocidTextJ to ocidTextF's stringByAppendingString:ocidTextG
set ocidTextJ to ocidTextJ's stringByAppendingString:""
set ocidTextJ to ocidTextJ's stringByAppendingString:ocidTextH
log ocidTextJ as text
log className() of ocidTextJ as text


####NSMutableString

set strTextF to "美しい日本語" as text
set strTextG to "" as text
set strTextH to "オープンタイプフォント" as text

set ocidMutableString to objNSMutableString's alloc()'s initWithCapacity:0
ocidMutableString's setString:strTextF
ocidMutableString's appendString:strTextG
ocidMutableString's appendString:strTextH
log ocidMutableString as text
log className() of ocidMutableString as text

ocidMutableString's setString:""
ocidMutableString's setString:strTextF
ocidMutableString's appendString:strTextG
ocidMutableString's appendString:""
ocidMutableString's appendString:strTextH
log ocidMutableString as text
log className() of ocidMutableString as text


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

|

[fig 02-03]フォルダの作成と削除

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

###ランダム番号
set numRandomNo to random number 100 as integer
###テキストにしておいて
set strRandomNo to numRandomNo as text
###フォルダの名前を定義
set strFolderName to "番号フォルダ" & strRandomNo as text
###名称未設定フォルダを作っておいて
tell application "Finder" to make new folder at desktop
###↑これのresult=名称未設定フォルダのエイリアス
set aliasNewFolder to the result as alias
###フォルダの名前を変更する
tell application "Finder" to set the name of aliasNewFolder to strFolderName
###3秒まって
delay 3
####削除する
tell application "Finder" to delete (folder strFolderName of folder (path to desktop folder from user domain))

(*
ゴミ箱に入れる場合は
tell application "Finder" to move (folder folder_name of folder (path to desktop folder from user domain)) to trash
*)




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

|

[fig 02-03]フォルダの作成 と 削除

#!/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
####ログ表示
doLogView()
####
property objMe : a reference to current application
property objNSString : a reference to objMe's NSString

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

####ランダム番号を生成
set numRandomNo to (random number 100) as integer
set numRandomNo to (random number from 0 to 100) as integer
set numRandomNo to (random number from 50 to 100) as integer

log numRandomNo
log class of numRandomNo

####フォルダ名を定義
set strFolderName to "番号フォルダ" & numRandomNo as text
log strFolderName as text
log class of strFolderName

#### フォルダ名をNSString
set ocidFolderName to objNSString's stringWithString:strFolderName
log ocidFolderName as text
log className() of ocidFolderName as text

set strDesktopPath to "~/Desktop/"

###ユーザーデスクトップフォルダを定義
set ocidDesktopPath to objNSString's stringWithString:strDesktopPath
log ocidDesktopPath as text
log className() of ocidDesktopPath as text

####フォルダ名を追加して作成するフォルダへのパスにする
set ocidMakeFolderPath to ocidDesktopPath's stringByAppendingString:ocidFolderName
log ocidMakeFolderPath as text
log className() of ocidMakeFolderPath as text

#####パス化する
set ocidPOSIXpath to ocidMakeFolderPath's stringByStandardizingPath
log ocidPOSIXpath as text
log className() of ocidPOSIXpath as text

####フォルダを作成
set boolMakeNewFolder to (objFileManager's createDirectoryAtPath:ocidPOSIXpath withIntermediateDirectories:true attributes:(missing value) |error|:(missing value))
log boolMakeNewFolder as boolean
log class of boolMakeNewFolder


###2秒まって
delay 2

###パスをNSStringに変換
set ocidPOSIXpathNSString to objNSString's stringWithString:ocidPOSIXpath
log ocidPOSIXpathNSString as text
log className() of ocidPOSIXpathNSString as text

###フォルダを削除
set boolGoToTrash to objFileManager's removeItemAtPath:ocidPOSIXpathNSString |error|:(missing value)
log boolGoToTrash as boolean
log class of boolGoToTrash as text

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

|

[fig 02-02]random number

#!/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
####ログ表示
doLogView()
####
property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
####
set numIntegerNo to (random number 100) as integer
set numIntegerNo to (random number from 0 to 100) as integer
set numIntegerNo to (random number from 50 to 100) as integer

log numIntegerNo
log class of numIntegerNo





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

|

その他のカテゴリー

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