AppleScript System Events

[System Events]RSSを取得してテキスト表示する


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

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

set strURL to ("https://www.nhk.or.jp/rss/news/cat0.xml") as text


########################
##【1】RSSのダウンロード
###最近の仕様でtemporary items folderを要求するとフォルダ生成される
tell application "System Events"
  set aliasDownloadsDirPath to (path to downloads folder from user domain) as alias
end tell
###念の為フォルダ作っておく
tell application "Finder"
  set boolExists to exists (folder "RssXml" of folder aliasDownloadsDirPath)
  if boolExists is false then
make new folder at aliasDownloadsDirPath with properties {name:"RssXml"}
  end if
  set aliasSaveDirPath to (folder "RssXml" of folder aliasDownloadsDirPath) as alias
  ##ランダムな番号をフォルダ名にして
  set strDirName to (random number from 100000 to 999999) as text
make new folder at aliasSaveDirPath with properties {name:strDirName}
  set aliasRssSaveDirPath to (folder strDirName of folder aliasSaveDirPath) as alias
end tell
set strSaveDirPath to (POSIX path of aliasRssSaveDirPath) as text
set strSaveFilePath to (strSaveDirPath & "cat0.xml") as text
###
set strCommandText to ("/usr/bin/curl -L \"" & strURL & "\" -o \"" & strSaveFilePath & "\"") as text
do shell script strCommandText
##
delay 1
########################
##【2】RSSの解析
set strOutPutText to ("") as text

tell application "System Events"
  tell XML file strSaveFilePath
    tell XML element "rss"
      tell XML element "channel"
        set listEveryItem to every XML element
        repeat with itemElement in listEveryItem
          tell itemElement
set listItemElement to every XML element
repeat with itemItemElement in listItemElement
tell itemItemElement
set strName to name as text
if strName is "title" then
set strValue to ("タイトル: " & value) as text
set strOutPutText to (strOutPutText & strValue & "\n") as text
else if strName is "link" then
set strValue to ("リンク: " & value) as text
set strOutPutText to (strOutPutText & strValue & "\n") as text
else if strName is "description" then
set strValue to ("詳細: " & value) as text
set strOutPutText to (strOutPutText & strValue & "\n") as text
end if
end tell
end repeat
          end tell
          set strOutPutText to (strOutPutText & "\n") as text
        end repeat
      end tell
    end tell
  end tell
end tell
set strOutPutText to strOutPutText as text
########################
##【3】テキスト表示
#ファイル名
set strFileName to ("index.html") as text
set strSaveHTMLFilePath to (strSaveDirPath & strFileName) as text
log strSaveHTMLFilePath
tell application "TextEdit"
activate
  set objNewDoc to make new document with properties {name:strFileName, path:strSaveHTMLFilePath}
  tell objNewDoc
activate
save in (POSIX file strSaveHTMLFilePath)
close saving yes
  end tell
end tell
tell application "TextEdit"
open (POSIX file strSaveHTMLFilePath)
  tell objNewDoc
    set its text to strOutPutText
    tell its text
properties
      set its font to "sans"
      set its color to {0, 0, 0}
      set its size to 16
    end tell
  end tell
  tell objNewDoc
save in (POSIX file strSaveHTMLFilePath)
  end tell
end tell

tell application "Finder"
make new Finder window to aliasSaveDirPath
activate
end tell

return


|

ファイル・フォルダの有無


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

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

set boolFileExist to doChkPathExists("/Applications/Safari.app/")
log boolFileExist

set boolFileExist to doChkPathExists("/Applications/実在しない.app/")
log boolFileExist



to doChkPathExists(argFilePath)
  set strFilePath to argFilePath as text
  tell application "System Events"
    if exists file strFilePath then
log "パスは有効です=パスが存在します"
return true
    else
log "パスは無効です=パスには存在しません"
return false
    end if
  end tell
end doChkPathExists


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

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


set boolFileExist to doChkPathExists("/Applications/Safari.app/")
log boolFileExist

set boolFileExist to doChkPathExists("/Applications/実在しない.app/")
log boolFileExist


to doChkPathExists(argFilePath)
  set strFilePath to argFilePath as text
  tell application "System Events"
    set boolItemExist to exists disk item strFilePath
    if boolItemExist is true then
log "パスは有効です=パスが存在します"
return true
    else
log "パスは無効です=パスには存在しません"
return false
    end if
  end tell
end doChkPathExists





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

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

##フォルダ
set boolFileExist to doChkPathExists("/Applications")
log boolFileExist
##アプリケーションはファイル扱い
set boolFileExist to doChkPathExists("/Applications/Safari.app/")
log boolFileExist
##シンボリックリンクはファイル扱い
set boolFileExist to doChkPathExists("/tmp")
log boolFileExist
##フォルダ 存在しない
set boolFileExist to doChkPathExists("/実在しないフォルダ/")
log boolFileExist
##フォルダ 存在しない
set boolFileExist to doChkPathExists("/Applications/サンプル.txt")
log boolFileExist


to doChkPathExists(argFilePath)
  set strFilePath to argFilePath as text
  tell application "System Events"
    set boolItemExist to exists disk item strFilePath
    if boolItemExist is true then
log "パスは有効です=パスが存在します"
      set classPath to class of disk item strFilePath
      if classPath is file then
log "パスはファイルです"
return true
      else if classPath is folder then
log "パスはフォルダです"
return false
      end if
    else
log "パスは無効です=パスには存在しません"
return false
    end if
  end tell
end doChkPathExists




|

[Character Viewer]文字ビューア

文字ビューア
アプリケーション毎に最後の状態を保持しているので
呼び出し方によって結果が異なります
Screen-20231125-162444

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

tell application "TextEdit"
  activate
  tell application "System Events"
    tell application process "TextEdit"
key code 49 using {control down, command down}
    end tell
  end tell
end tell


tell application "Finder"
  activate
  tell application "System Events"
    tell application process "Finder"
key code 49 using {control down, command down}
    end tell
  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 framework "AppKit"
use framework "UniformTypeIdentifiers"
use scripting additions

property refMe : a reference to current application

set strBundleID to "com.apple.systempreferences" as text

############################
###【事前1】システム設定の起動を確定させる
set ocidRunAppArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)
if (count of ocidRunAppArray) ≠ 0 then
  log "起動中です"
  tell application id strBundleID to activate
else
  ####ゾンビ対策終了させてから処理させる
  tell application id strBundleID
    set numCntWindow to (count of every window) as integer
  end tell
  if numCntWindow = 0 then
    tell application id strBundleID to quit
  else
    tell application id strBundleID
      close (every window)
      quit
    end tell
  end if
  ####半ゾンビ化対策
  set ocidRunningApplication to refMe's NSRunningApplication
  set ocidAppArray to ocidRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)
  repeat with itemAppArray in ocidAppArray
itemAppArray's terminate
  end repeat
  ###システム設定を起動させる
  try
    tell application id strBundleID to activate
  on error
    tell application "System Settings" to activate
  end try
  ###起動待ち
  tell application id strBundleID
    ###起動確認 最大10秒
    repeat 10 times
      activate
      set boolFrontMost to frontmost as boolean
      log boolFrontMost
      if boolFrontMost is true then
###魔法の1秒
delay 0.5
exit repeat
      else
delay 0.5
      end if
    end repeat
  end tell
end if


tell application "System Settings"
  tell application "System Events"
    tell application process "System Settings"
      set strName to (name of front window) as text
    end tell
  end tell
end tell

log strName as text

|

[System Events]アプリケーションを隠す(非表示)


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

tell application id "com.apple.clock" to launch

tell application id "com.apple.clock"
  set strAppName to name as text
  log strAppName
end tell

###表示
tell application "System Events"
  tell application process strAppName
    set visible to true
  end tell
end tell
delay 1

###非表示
tell application "System Events"
  tell application process strAppName
    set visible to false
  end tell
end tell

|

[System Events]コピーペースト


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


tell application "Finder"
  activate
  ## Copy
  tell application "System Events"
    key code 8 using command down
  end tell
  
  ## Paste
  tell application "System Events"
    key code 9 using command down
  end tell
  
end tell


tell application "Finder"
  activate
  ## Copy
  tell application "System Events"
    keystroke "c" using command down
  end tell
  
  ## Paste
  tell application "System Events"
    keystroke "v" using command down
  end tell
  
end tell



|

KeyCode3種(デベロッパツールを開く)


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

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


tell application id "com.apple.Safari"
  set numCntWin to (count of every window) as integer
end tell

if numCntWin = 0 then
  log "ウィンドがありません"
return "ウィンドがありません"
end if

tell application id "com.apple.Safari"
  activate
  tell application "System Events"
    tell process "Safari"
key code 34 using {command down, option down}
    end tell
  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


tell application id "com.apple.Safari"
  set numCntWin to (count of every window) as integer
end tell

if numCntWin = 0 then
  log "ウィンドがありません"
return "ウィンドがありません"
end if

tell application id "com.apple.Safari"
  activate
  tell application "System Events"
    tell process "Safari"
key down {command}
key down {option}
      keystroke "i"
key up {option, command}
      
      
    end tell
  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


tell application id "com.apple.Safari"
  set numCntWin to (count of every window) as integer
end tell

if numCntWin = 0 then
  log "ウィンドがありません"
return "ウィンドがありません"
end if

tell application id "com.apple.Safari"
  activate
  tell application "System Events"
    tell process "Safari"
      keystroke "i" using {command down, option down}
    end tell
  end tell
end tell

|

ボリューム(ディスク)のフォーマットを調べる


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


###パス
set strPath to "/" as text
###エイリアス
set aliasPath to POSIX file strPath as alias

tell application "Finder"
  ###エイリアスを分解
  set fileFolderPath to folder aliasPath
  ###ディスク部分を取得
  set refDiskName to disk of fileFolderPath
  ###起動ディスク配下
  if refDiskName is startup disk then
    tell startup disk
      ##ディスク(ボリューム)名
      set strVolumeName to name as text
    end tell
  else
    ##ディスク(ボリューム)名
    set strVolumeName to refDiskName as text
  end if
end tell

tell application "System Events"
  ##ディスク(ボリューム)名
  tell disk strVolumeName
    ###フォーマットを取得
    set strDiskFormat to format as text
    if strDiskFormat contains "Mac" then
    else if strDiskFormat contains "NFS" then
    else if strDiskFormat contains "APFS" then
    else if strDiskFormat contains "Apple" then
      
    else if strDiskFormat contains "unknown" then
    else if strDiskFormat contains "DOS" then
    else if strDiskFormat contains "FAT" then
    else
      log "それ以外"
    end if
  end tell
end tell



|

[UI Element Scripts]スクリーンショットを撮る


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

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

##Previewを起動
tell application "Preview" to launch
##前面に
tell application "Preview" to activate
##UIスクリプト
tell application "System Events"
  tell process "Preview"
    tell menu "ファイル" of menu bar 1
click
      tell menu item "スクリーンショットを撮る"
click
click menu item "ウインドウ…" of menu "スクリーンショットを撮る"
      end tell
    end tell
  end tell
end tell


|

[VSCode]前面ドキュメントのパスコピー(System Events)


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

#!/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"
###NSPasteboardはAppkitなので必須
use framework "AppKit"
use scripting additions

property refMe : a reference to current application


################################
######ペーストボード初期化
################################
set the clipboard to "" as text
##初期化
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
ocidPasteboard's clearContents()
delay 0.1
####パス取得を試みる
doCopyPathVsCode()
delay 0.1
################################
######ペーストボード取得
################################
##可変テキストとして受け取る
set ocidPasteboardArray to ocidPasteboard's readObjectsForClasses:({refMe's NSMutableString}) options:(missing value)
##シングルARRAYが空ならペーストボードの中身が画像とか他のものなのでエラーで終了
if (count of ocidPasteboardArray) = 0 then
  delay 0.1
  ###失敗したら再度トライ
doCopyPathVsCode()
  delay 0.1
  set ocidPasteboardArray to ocidPasteboard's readObjectsForClasses:({refMe's NSMutableString}) options:(missing value)
  ###ペーストボードの中身をテキストで確定
  set strPasteboardStrings to (ocidPasteboardArray's objectAtIndex:0) as text
else
  ###ペーストボードの中身をテキストで確定
  set strPasteboardStrings to (ocidPasteboardArray's objectAtIndex:0) as text
end if

if (strPasteboardStrings starts with "/") = false then
  ###パスの取得に失敗したのでもう一度トライする
  delay 0.1
  ####パス取得を試みる
doCopyPathVsCode()
  delay 0.1
  set ocidPasteboardArray to ocidPasteboard's readObjectsForClasses:({refMe's NSMutableString}) options:(missing value)
  ###ペーストボードの中身をテキストで確定
  set strPasteboardStrings to (ocidPasteboardArray's objectAtIndex:0) as text
end if

log strPasteboardStrings



################################
######パスコピーVScode
################################

to doCopyPathVsCode()
  ###前面に出して
  tell application "System Events"
    launch
  end tell
  tell application "System Events"
    activate
  end tell
  tell application id "com.microsoft.VSCode"
    activate
  end tell
  delay 0.1
  ###パス取得
  tell application "System Events"
key down {command}
    keystroke "k"
key up {command}
    delay 0.1
key down {option, command}
    keystroke "c"
key up {option, command}
  end tell
  
end doCopyPathVsCode

|

その他のカテゴリー

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