AppleScript do shell script

[do shell script]他の言語を利用する ruby swift Python perl zsh bash

python3

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

#!/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 strText to ("HELLO WORLD") as text

set strCommandText to ("/usr/bin/python3 -c 'print( \"" & strText & "\".lower())'") as text
set strResponse to (do shell script strCommandText) as text
log strResponse
return strResponse

perl

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

#!/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 strText to ("HELLO WORLD") as text

set strCommandText to ("/usr/bin/perl -pe '$_ = lc($_)' <<< \"" & strText & "\"") as text
set strResponse to (do shell script strCommandText)as text
log strResponse
return strResponse

swift

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

#!/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 strText to ("HELLO WORLD") as text

set strCommandText to ("/usr/bin/swift -e 'let strText = \"" & strText & "\";print(strText.lowercased())'") as text
set strResponse to (do shell script strCommandText) as text
log strResponse
return strResponse

ruby

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

#!/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 strText to ("HELLO WORLD") as text

set strCommandText to ("/usr/bin/ruby -e 'puts \"" & strText & "\".downcase'") as text
set strResponse to (do shell script strCommandText) as text
log strResponse
return strResponse

bash

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

#!/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 strText to ("HELLO WORLD") as text

set strCommandText to ("/bin/bash -c '/bin/echo \"" & strText & "\" | tr '[:upper:]' '[:lower:]''") as text
set strResponse to (do shell script strCommandText) as text
log strResponse
return strResponse

zsh

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

#!/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 strText to ("HELLO WORLD") as text

set strCommandText to ("/bin/zsh -c '/bin/echo \"" & strText & "\"| tr \"[:upper:]\" \"[:lower:]\"'") as text
set strResponse to (do shell script strCommandText) as text
log strResponse
return strResponse

|

[shell script]Permission コマンドで ファイル フォルダのアクセス権を設定変更する


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

#!/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 strFilePath to ("~/Library/Mail/PersistenceInfo.plist") as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set strFilePath to ocidFilePath as text
set strCommandText to ("/bin/chmod 700 \"" & strFilePath & "\"") as text
try
  set strResponse to (do shell script strCommandText) as text
on error
  log "アクセス権設定でエラーになりました"
end try

###フォルダに対してのアクセス権設定
set strDirPath to ("~/Library/Mail/") as text
set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
set strDirPath to ocidDirPath as text
set strCommandText to ("") as text
set strCommandText to ("/bin/chmod 700 \"" & strDirPath & "\"") as text
try
  set strResponse to (do shell script strCommandText) as text
on error
  log "アクセス権設定でエラーになりました"
end try



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

#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
#################################################
###ファイル
STR_FILE_PATH="$HOME/Library/Mail/PersistenceInfo.plist"
/bin/chmod 700 "$STR_FILE_PATH"

if ! /bin/chmod -f 700 "$STR_FILE_PATH"; then
  echo "$STR_FILE_PATH""のアクセス権の設定変更に失敗しました"
fi
####ディレクトリ
STR_DIR_PATH="$HOME/Library/Mail/"
/bin/chmod 700 "$STR_DIR_PATH"

if ! /bin/chmod -f 700 "$STR_DIR_PATH"; then
  echo "$STR_DIR_PATH""のアクセス権の設定変更に失敗しました"
fi
exit 0




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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
(*
事前準備
/private/etc/pam.d/sudo_local
# sudo_local: local config file which survives system update and is included for sudo
# uncomment following line to enable Touch ID for sudo
#auth sufficient pam_tid.so
この#を取ります
https://quicktimer.cocolog-nifty.com/icefloe/2023/10/post-b3346a.html
*)
#
#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 recordSystemInfo to system info
set strUID to (short user name of recordSystemInfo) as text
########################
########MacOS13まで
########################
#### ファイルに対してのアクセス権設定
set strFilePath to ("/Library/WebServer/Documents/index.html.en") as text
set strCommandText to ("/bin/chmod 775 \"" & strFilePath & "\"") as text
try
  set strResponse to (do shell script strCommandText) as text
on error
  set strCommandText to ("/usr/bin/sudo /bin/chmod 775 \"" & strFilePath & "\"") as text
  set strResponse to (do shell script strCommandText user name strUID with prompt "管理者権限が必要です" with administrator privileges) as text
end try
###フォルダに対してのアクセス権設定
set strDirPath to ("/Library/WebServer/Documents") as text

set strCommandText to ("") as text
set strCommandText to ("/bin/chmod 777 \"" & strDirPath & "\"") as text
try
  set strResponse to (do shell script strCommandText) as text
on error
  set strCommandText to ("/usr/bin/sudo /bin/chmod 777 \"" & strDirPath & "\"") as text
  set strResponse to (do shell script strCommandText user name strUID with prompt "管理者権限が必要です" with administrator privileges) as text
end try

########################
########MacOS14から
########################
#### ファイルに対してのアクセス権設定
set strFilePath to ("/Library/WebServer/Documents/index.html.en") as text
set strCommandText to ("/bin/chmod 775 \"" & strFilePath & "\"") as text
try
  set strResponse to (do shell script strCommandText) as text
on error
  set strCommandText to ("/usr/bin/sudo /bin/chmod 775 \"" & strFilePath & "\"") as text
  set strResponse to (do shell script strCommandText with administrator privileges) as text
end try

###フォルダに対してのアクセス権設定
set strDirPath to ("/Library/WebServer/Documents") as text

set strCommandText to ("") as text
set strCommandText to ("/bin/chmod 777 \"" & strDirPath & "\"") as text
try
  set strResponse to (do shell script strCommandText) as text
on error
  set strCommandText to ("/usr/bin/sudo /bin/chmod 777 \"" & strDirPath & "\"") as text
  set strResponse to (do shell script strCommandText with administrator privileges) as text
end try


|

[AppleScript] do shell scriptでTouchIDを使う(macOS14対応)


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

(*
事前準備
/private/etc/pam.d/sudo_local
# sudo_local: local config file which survives system update and is included for sudo
# uncomment following line to enable Touch ID for sudo
#auth sufficient pam_tid.so
この#を取ります
https://quicktimer.cocolog-nifty.com/icefloe/2023/10/post-b3346a.html

*)

## OS13まで(with administrator privilegesがTouch IDを呼び出していましたが)
set strCommandText to "/usr/bin/sudo /bin/date" as text
do shell script strCommandText with prompt "管理者権限が必要です" with administrator privileges


## OS14から(/usr/bin/sudoのTouch IDの呼び出しにまかせるに変わります)
set strCommandText to "/usr/bin/sudo /bin/date" as text
do shell script strCommandText


(*
OS13まではAppleScriptからTouch IDを呼び出していましたが
OS14かららシェルの/usr/bin/sudoがTouch IDを呼び出すイメージです
with administrator privilegesは使わない
with administrator privileges使う場面は
ユーザーを指定する場合のみになったって事かな?
*)

|

[launchctl]不要な起動項目を削除する

#!/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 objFileManager to refMe's NSFileManager's defaultManager()

###デフォルト
set ocidUserLibraryPath to (objFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSLocalDomainMask))
set ocidDirPathURL to item 1 of ocidUserLibraryPath
set ocidDirPathURL to ocidDirPathURL's URLByAppendingPathComponent:"LaunchDaemons" isDirectory:true
set aliasDefaultLocation to ocidDirPathURL as alias
####ダイアログ
set listChooseFileUTI to {"com.apple.property-list"}
set strPromptText to "ファイルを選んでください" as text
set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with invisibles and multiple selections allowed without showing package contents)

###ゴミ箱
set ocidUserTrashPath to (objFileManager's URLsForDirectory:(refMe's NSTrashDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserTrashPathURL to item 1 of ocidUserTrashPath
set strUserTrashPathURL to ocidUserTrashPathURL's |path|() as text

###選んだファイルの数だけ繰り返し
repeat with objAliasFilePath in listAliasFilePath
    set strFilePath to POSIX path of objAliasFilePath as text
    try
        ###停止
        set strCommandText to "/usr/bin/sudo /bin/launchctl stop -w  \"" & strFilePath & "\"" as text
        do shell script strCommandText with administrator privileges
    end try
    try
        ###アンロード
        set strCommandText to "/usr/bin/sudo /bin/launchctl unload -w  \"" & strFilePath & "\"" as text
        do shell script strCommandText with administrator privileges
    end try
    try
        ####削除
        set strCommandText to "/usr/bin/sudo /bin/launchctl remove -w \"" & strFilePath & "\"" as text
        do shell script strCommandText with administrator privileges
    end try
    ####ゴミ箱へ
    set strCommandText to "/usr/bin/sudo /bin/mv  \"" & strFilePath & "\"  \"" & strUserTrashPathURL & "\"" as text
    do shell script strCommandText with administrator privileges
    
end repeat

#!/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 objFileManager to refMe's NSFileManager's defaultManager()

###デフォルト
set ocidUserLibraryPath to (objFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDirPathURL to item 1 of ocidUserLibraryPath
set ocidDirPathURL to ocidDirPathURL's URLByAppendingPathComponent:"LaunchAgents" isDirectory:true
set aliasDefaultLocation to ocidDirPathURL as alias
####ダイアログ
set listChooseFileUTI to {"com.apple.property-list"}
set strPromptText to "ファイルを選んでください" as text
set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with invisibles and multiple selections allowed without showing package contents)

###ゴミ箱
set ocidUserTrashPath to (objFileManager's URLsForDirectory:(refMe's NSTrashDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserTrashPathURL to item 1 of ocidUserTrashPath
set strUserTrashPathURL to ocidUserTrashPathURL's |path|() as text

###選んだファイルの数だけ繰り返し
repeat with objAliasFilePath in listAliasFilePath
    set strFilePath to POSIX path of objAliasFilePath as text
    try
        ###停止
        set strCommandText to "/usr/bin/sudo /bin/launchctl stop -w  \"" & strFilePath & "\"" as text
        do shell script strCommandText with administrator privileges
    end try
    try
        ###アンロード
        set strCommandText to "/usr/bin/sudo /bin/launchctl unload -w  \"" & strFilePath & "\"" as text
        do shell script strCommandText with administrator privileges
    end try
    try
        ####削除
        set strCommandText to "/usr/bin/sudo /bin/launchctl remove -w \"" & strFilePath & "\"" as text
        do shell script strCommandText with administrator privileges
    end try
    ####ゴミ箱へ
    set strCommandText to "/usr/bin/sudo /bin/mv  \"" & strFilePath & "\"  \"" & strUserTrashPathURL & "\"" as text
    do shell script strCommandText with administrator privileges
    
end repeat

|

その他のカテゴリー

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