AppleScript List

[AppleScript] リスト形式で項目を先頭に追加 項目を最後についか


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use scripting additions
008
009
010#リストの先頭に追加
011set listAddItem to {"BBB", "CCC", "DDD"} as list
012copy "AAA" to beginning of listAddItem
013log listAddItem as list
014-->(*AAA, BBB, CCC, DDD*)
015
016#リストの先頭に追加
017set listAddItem to {"BBB", "CCC", "DDD"} as list
018set beginning of listAddItem to   "AAA" 
019log listAddItem as list
020-->(*AAA, BBB, CCC, DDD*)
021
022
023#リストの後方に追加
024set listAddItem to {"BBB", "CCC", "DDD"} as list
025copy  "AAA" to end of listAddItem
026log listAddItem
027-->(*BBB, CCC, DD, AAA*)
028
029
030
031#リストの後方に追加
032set listAddItem to {"BBB", "CCC", "DDD"} as list
033set end of listAddItem to "AAA"
034log listAddItem
035-->(*BBB, CCC, DD, AAA*)
036
037
038#リストの前方に追加
039set listAddItem to {"BBB", "CCC", "DDD"} as list
040set listAddItem to {"AAA"} & listAddItem as list
041log listAddItem as list
042-->(*AAA, BBB, CCC, DDD*)
043
044#リストの後方に追加
045set listAddItem to {"BBB", "CCC", "DDD"} as list
046set listAddItem to listAddItem & {"AAA"} as list
047log listAddItem as list
048-->(*BBB, CCC, DD, AAA*)
049
050#最初を置換
051set listAddItem to {"BBB", "CCC", "DDD"} as list
052copy "AAA" to first item of listAddItem
053log listAddItem as list
054-->(*AAA, CCC, DDD*)
055
056#2番目を置換
057set listAddItem to {"BBB", "CCC", "DDD"} as list
058copy "AAA" to item 2 of listAddItem
059log listAddItem as list
060-->(*BBB, CCC, AAA*)
061
062#最後を置換
063set listAddItem to {"BBB", "CCC", "DDD"} as list
064copy "AAA" to last item of listAddItem
065log listAddItem as list
066-->(*BBB, AAA, DDD*)
067
068
069
AppleScriptで生成しました

|

[AppleScript] リストの最初の項目 最後の項目の削除


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use scripting additions
008
009set aliasDirPath to (path to fonts from user domain) as alias
010
011set aliasChooseDirPath to (choose folder default location aliasDirPath) as alias
012
013
014set strDirPath to (POSIX path of aliasChooseDirPath) as text
015log strDirPath
016
017set strDelim to AppleScript's text item delimiters
018set AppleScript's text item delimiters to "/"
019set listDirName to every text item of strDirPath
020set AppleScript's text item delimiters to strDelim
021
022#最初の項目の削除
023if (first item of listDirName) is "" then
024  set listDirName to (items 2 thru -1 of listDirName) as list
025end if
026#最後の項目の削除
027if (last item of listDirName) is "" then
028  set listDirName to (items 1 thru -2 of listDirName) as list
029end if
030
031log (count of listDirName) as integer
032
033
034repeat with itemDirName in listDirName
035  log itemDirName as text
036end repeat
AppleScriptで生成しました

|

ネストされたリストをカンマ区切りに(子要素あり)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004
005# com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.6"
008use framework "Foundation"
009use scripting additions
010
011property refMe : a reference to current application
012
013set listInput to {{1, 2}, {3, 4}} as list
014
015log doDisChildArray(listInput) as list
016-->{1, 2, 3, 4}
017##############################
018# {{x,y},{w,h}} を{x,y,w,h}
019##############################
020to doDisChildArray(argList)
021  set listOutPut to {} as list
022  repeat with itemChildArray in argList
023    repeat with itemArray in itemChildArray
024      copy itemArray to end of listOutPut
025    end repeat
026  end repeat
027  set ocidArray to refMe's NSArray's arrayWithArray:(listOutPut)
028  #カンマでつないでテキストに
029  set ocidCSV to (ocidArray's componentsJoinedByString:(","))
030  set strCSV to ocidCSV as text
031  return strCSV
032end doDisChildArray
033
AppleScriptで生成しました

|

リストをカンマ区切りに


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
005use AppleScript version "2.6"
006use framework "Foundation"
007use scripting additions
008
009property refMe : a reference to current application
010
011set listInput to {1, 2, 3, 4} as list
012
013log doListToCSV(listInput) as text
014-->"1,2,3,4"
015##############################
016# リスト{x,y,w,h}をテキストの"x,y,w,h"に
017##############################
018to doListToCSV(argList)
019  #Arrayにして
020  set ocidArray to refMe's NSArray's arrayWithArray:(argList)
021  #カンマでつないでテキストに
022  set ocidCSV to (ocidArray's componentsJoinedByString:(","))
023  set strCSV to ocidCSV as text
024  return strCSV
025end doListToCSV
026
AppleScriptで生成しました

|

ファイル名の英数小文字を大文字にする


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


(*
旧OS用なので
Objective-cを使わないクラシックな処理
shellも使わない
*)
###ダブルクリックして開いたら?
on run
  set strMes to "選んでください"
  set aliasDefaultsLocation to (POSIX file "/Users/Shared/Downloads") as alias
  set listUTI to {"public.movie", "public.mpeg-4", "public.mpeg-4-audio", "com.apple.quicktime-movie"} as list
  set listAliasFilePath to (open (choose file strMes default location aliasDefaultsLocation with prompt strMes of type listUTI with invisibles and multiple selections allowed without showing package contents)) as list
open
end run

###ドロップレットの始まり
on open listAliasFilePath
  ###受け取ったエイリアスの数だけ繰り返し
  repeat with itemAliasFilePath in listAliasFilePath
    set aliasFilePath to itemAliasFilePath as alias
    ###フォルダチェック
    tell application "Finder"
      set strKind to (kind of aliasFilePath) as text
    end tell
    ###
    if strKind is "フォルダ" then
      log "フォルダは処理しない"
    else
      ###ファイルは処理する
      tell application "Finder"
# set aliasContainerDirPath to (container of aliasFilePath) as alias
###ファイル名と拡張子名
set strFileName to (name of aliasFilePath) as text
set strExtension to (name extension of aliasFilePath) as text
      end tell
      ###ファイル名からベースファイル名を取得
      set AppleScript's text item delimiters to "."
      set listFileName to every text item of strFileName
      set AppleScript's text item delimiters to ""
      set numCntChar to (count of listFileName) as integer
      set strBaseFileName to ("") as text
      repeat with itemIntNo from 1 to (numCntChar - 1) by 1
set strBaseFileName to (strBaseFileName & (item itemIntNo of listFileName)) as text
      end repeat
      ###本処理 小文字を大文字に
      set listLowerAlphabet to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} as list
      set listUpperAlphabet to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} as list
      set AppleScript's text item delimiters to ""
      set listNewFileName to every text item of strTmpBaseFileName
      set AppleScript's text item delimiters to ""
      #変更後のファイル名
      set strNewFileName to ("") as text
      #全文字繰り返し
      repeat with itemNewFileName in listNewFileName
#テキストに
set strItemNewFileName to itemNewFileName as text
#小文字リストに値があれば処理する
if listLowerAlphabet contains strItemNewFileName then
#小文字リストの何番目?
set cntNo to doOffsetInList(strItemNewFileName, listLowerAlphabet) as integer
#大文字リストから大文字を取り出してファイル名にする
set strItemNewFileName to (item cntNo of listUpperAlphabet) as text
end if
#変換した文字も変換しない文字も積み上げていく
set strNewFileName to (strNewFileName & strItemNewFileName) as text
      end repeat
      ##拡張子を加えて最終的な変更後のファイル名
      set strNewFileName to (strNewFileName & "." & strExtension) as text
      tell application "Finder"
tell file aliasFilePath
###ファイル名の変更
set name to strNewFileName
end tell
      end tell
    end if
  end repeat
end open



##################
#リストの何番目?
##################
to doOffsetInList(argText, argList)
  set numCntPosition to 1 as integer
  repeat with itemChooser in argList
    set strItemChooser to itemChooser as text
    if strItemChooser is argText then
      set numCntOffset to numCntPosition as integer
    end if
    set numCntPosition to numCntPosition + 1 as integer
  end repeat
return numCntOffset
end doOffsetInList
##################
#文字の置換
##################
to doReplace(argOrignalText, argSearchText, argReplaceText)
  set strDelim to AppleScript's text item delimiters
  set AppleScript's text item delimiters to argSearchText
  set listDelim to every text item of argOrignalText
  set AppleScript's text item delimiters to argReplaceText
  set strReturn to listDelim as text
  set AppleScript's text item delimiters to strDelim
return strReturn
end doReplace


|

[リスト]list基本


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

set listArray to {"AAA", "BBB", "CCC", "DDD"} as list
###項目の有無
if listArray contains "CCC" then
  log "リストの中に値があります"
end if
###項目の有無
set boolContains to (listArray contains "CCC") as boolean
if boolContains is true then
  log "リストの中に値があります"
end if
###項目の有無
if "CCCCCCCC" is in listArray then
  log "リストの中に値があります"
else
  log "リストの中に値がありません"
end if
###項目の有無
set boolIsIn to ("CCCCCCCC" is in listArray) as boolean
if boolIsIn is true then
  log "リストの中に値があります"
else
  log "リストの中に値がありません"
end if


###最初の
log item 1 of listArray
-->(*AAA*)

###2番目と3番目
log items 2 thru 3 of listArray
-->(*BBB, CCC*)

###2番目から最後まで
log items 2 thru (last item) of listArray
-->(*BBB, CCC, DDD*)

###最初の
log (first item) of listArray
-->(*AAA*)

###2番目の
log (second item) of listArray
-->(*BBB*)

###最後の
log (last item) of listArray
-->(*DDD*)

##後ろから 1番目
log item -1 of listArray
-->(*DDD*)

##後から2番目から最後まで
log items -2 thru (last item) of listArray
-->(*CCC, DDD*)

##後から2番目から最初まで
log items -2 thru (first item) of listArray
-->(*AAA, BBB, CCC*)

##後に追加
copy "EEE" to end of listArray
-->(*AAA, BBB, CCC, DDD, EEE*)

##後に追加
set end of listArray to "FFF"
-->(*AAA, BBB, CCC, DDD, EEE, FFF*)

###項目追加して新しいリストに
set listNewList to (listArray & "GGG") as list
-->{"AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"}

###値の変更 最初の項目
set (first item) of listNewList to "aaa"
-->(*aaa, BBB, CCC, DDD, EEE, FFF, GGG*)





|

リストの値を入れ替える

リストの各値を10づつ加算します

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

###オフセット値
set numOffSet to 10 as integer
###最初のウィンドウの位置
set listBounds to {0, 25, 620, 360} as list


###ウィンドウの数だけ繰返し
repeat 20 times
  
  ###ウィンドウの位置を次のウィンドウ用に加算
  ###値を取って
  set numFirst to (first item of listBounds) as integer
  set numSecond to (second item of listBounds) as integer
  set numThird to (third item of listBounds) as integer
  set numFourth to (fourth item of listBounds) as integer
  ###加算して
  set numFirst to numFirst + numOffSet as integer
  set numSecond to numSecond + numOffSet as integer
  set numThird to numThird + numOffSet as integer
  set numFourth to numFourth + numOffSet as integer
  ###リストに戻す
  set (first item of listBounds) to numFirst as integer
  set (second item of listBounds) to numSecond as integer
  set (third item of listBounds) to numThird as integer
  set (fourth item of listBounds) to numFourth as integer
  
  log listBounds
  -->
  (*10, 35, 630, 370*)
  (*20, 45, 640, 380*)
  (*30, 55, 650, 390*)
  (*40, 65, 660, 400*)
  (*50, 75, 670, 410*)
  (*60, 85, 680, 420*)
  (*70, 95, 690, 430*)
  (*80, 105, 700, 440*)
  (*90, 115, 710, 450*)
  (*100, 125, 720, 460*)
  (*110, 135, 730, 470*)
  (*120, 145, 740, 480*)
  (*130, 155, 750, 490*)
  (*140, 165, 760, 500*)
  (*150, 175, 770, 510*)
  (*160, 185, 780, 520*)
  (*170, 195, 790, 530*)
  (*180, 205, 800, 540*)
  (*190, 215, 810, 550*)
  (*200, 225, 820, 560*)
  
end repeat

|

[List]リストの何番目?



set listSmaple to {"AA", "BB", "CC", "DD", "EE", "FF", "", "GG"} as list


set strSerchText to "DD" as text


log doOffsetInList(strSerchText, listSmaple)

-->(*4*)


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

######リストの何番目?サブ

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


to doOffsetInList(argText, argList)

  set numCntPosition to 1 as integer

  repeat with itemChooser in argList

    set strItemChooser to itemChooser as text

    if strItemChooser is argText then

      set numCntOffset to numCntPosition as integer

    end if

    set numCntPosition to numCntPosition + 1 as integer

  end repeat

  return numCntOffset

end doOffsetInList

|

[リスト]list基本

set listArray to {"AAA", "BBB", "CCC", "DDD"} as list

log item 1 of listArray
-->(*AAA*)

log items 2 thru 3 of listArray
-->(*BBB, CCC*)

log items 2 thru (last item) of listArray
-->(*BBB, CCC, DDD*)

log (first item) of listArray
-->(*AAA*)

log (second item) of listArray
-->(*BBB*)

log (last item) of listArray
-->(*DDD*)

##マイナスは後ろから
log item -1 of listArray
-->(*DDD*)

##マイナスは後ろから
log item -2 of listArray
-->(*CCC*)

|

[Basic]リスト形式色々

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


###テキストが入っているリスト
set listOfText to {"AAA", "BBB", "CCC", "DDD", "EEE"} as list

###数値が入っているリスト
set listOfNumber to {1, 2, 3, 4, 5} as list

###リストの中にリストが入ってるリスト
set listOfDouble to {listOfText, listOfNumber} as list
-->{{"AAA", "BBB", "CCC", "DDD", "EEE"} ,{1, 2, 3, 4, 5} }

###リストの中にレコードが入ってるリスト
set recordA to {UserName:"林檎太郎", UserAge:23} as record
set recordB to {UserName:"大江須二郎", UserAge:24} as record

set listOfRecord to {recordA, recordB} as list
log listOfRecord as list
-->(*UserName:林檎太郎, UserAge:23, UserName:大江須二郎, UserAge:24*)

###################
##ディスクオブジェクトのリスト
tell application "System Events"
set listDisk to every disk
end tell
--> {disk id "Macintosh HD,-100,2", disk id "VM,-101,2", disk id "Preboot,-102,2", disk id "Update,-103,2", disk id "xarts,-104,2", disk id "iSCPreboot,-105,2", disk id "Hardware,-106,2", disk id "home,-107,2"}
log item 1 of listDisk

###################
##オブジェクへの参照のリスト
tell application "Finder"
tell folder (path to music folder from user domain)
set listObjAlias to (every item) as list
end tell
end tell
log item 1 of listObjAlias as alias
--> (*alias Macintosh HD:Users:XXXXXXXXXXX:Music:Music:*)

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

|

その他のカテゴリー

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