Admin LaunchServices

launchctlのplistファイルを0バイト(空)にしてロックする

ダウンロード - 0バイトロック.zip

|

[bash]lsregisterに不具合が発生した場合のリセット

202406100845281430x94022
サンプルコード

サンプルソース(参考)
行番号ソース
001#!/bin/bash
002#com.cocolog-nifty.quicktimer.icefloe
003#################################################
004###管理者インストールしているか?チェック
005USER_WHOAMI=$(/usr/bin/whoami)
006/bin/echo "実行ユーザー(whoami): $USER_WHOAMI"
007if [ "$USER_WHOAMI" != "root" ]; then
008  /bin/echo "このスクリプトを実行するには管理者権限が必要です。"
009  /bin/echo "sudo で実行してください"
010  ### path to me
011  SCRIPT_PATH="${BASH_SOURCE[0]}"
012  /bin/echo "/usr/bin/sudo \"$SCRIPT_PATH\""
013  /bin/echo "↑を実行してください"
014  exit 1
015else
016  ###実行しているユーザー名
017  CONSOLE_USER=$(/bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }')
018  /bin/echo "コンソールユーザー(scutil): $CONSOLE_USER"
019  ###実行しているユーザー名
020  HOME_USER=$(/bin/echo "$HOME" | /usr/bin/awk -F'/' '{print $NF}')
021  /bin/echo "実行ユーザー(HOME): $HOME_USER"
022  ###logname
023  LOGIN_NAME=$(/usr/bin/logname)
024  /bin/echo "ログイン名(logname): $LOGIN_NAME"
025  ###UID
026  USER_NAME=$(/usr/bin/id -un)
027  /bin/echo "ユーザー名(id): $USER_NAME"
028  ###STAT
029  STAT_USR=$(/usr/bin/stat -f%Su /dev/console)
030  /bin/echo "STAT_USR(console): $STAT_USR"
031fi
032
033########################################
034##"system", "local", "network" "user"
035/bin/echo "チェック開始"
036STR_CMD_PATH="/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
037#書き出し先フォルダを作っておいて
038STR_SAVE_DIR_PATH="/Users/${STAT_USR}/Documents/Apple/Lsregister"
039/usr/bin/sudo -u "$STAT_USR" /bin/mkdir -p "$STR_SAVE_DIR_PATH"
040/usr/bin/sudo -u "$STAT_USR" /usr/bin/touch "$STR_SAVE_DIR_PATH/_このフォルダは削除しても大丈夫です.txt"
041STR_DATE_NO=$(/bin/date "+%m%d%H%M%Y")
042#チェック
043"$STR_CMD_PATH" -lint
044#ダンプします
045STR_SAVE_FILE_NAME="$STR_DATE_NO""_all.txt"
046STR_SAVE_FILE_PATH="${STR_SAVE_DIR_PATH}/${STR_SAVE_FILE_NAME}"
047"$STR_CMD_PATH" -dump | /usr/bin/sudo -u "$STAT_USR" tee -a "$STR_SAVE_FILE_PATH" >/dev/null
048#UTIを抽出
049STR_EXP_FILE_NAME="$STR_DATE_NO""_UTI.txt"
050STR_EXP_FILE_PATH="${STR_SAVE_DIR_PATH}/${STR_EXP_FILE_NAME}"
051/usr/bin/grep '^type id' "$STR_SAVE_FILE_PATH" | /usr/bin/sudo -u "$STAT_USR" tee -a "$STR_EXP_FILE_PATH" >/dev/null
052#名前とパスを抽出
053STR_EXP_FILE_NAME="$STR_DATE_NO""_NAME.txt"
054STR_EXP_FILE_PATH="${STR_SAVE_DIR_PATH}/${STR_EXP_FILE_NAME}"
055/usr/bin/grep -E "path:|name: | more" "$STR_SAVE_FILE_PATH" | /usr/bin/sudo -u "$STAT_USR" tee -a "$STR_EXP_FILE_PATH" >/dev/null
056#
057/bin/echo "再構築を実行しますか?"
058/bin/echo "Yを選択すると再起動します"
059/bin/echo "未保存の書類がある場合はNを入力してください"
060/bin/echo "Nを選択するとこのまま処理を終了します"
061/bin/echo "(Y/N)"
062
063read -r STR_USER_ANS
064# 入力に基づいて処理を分岐
065if [[ "$STR_USER_ANS" =~ ^[Yy]$ ]]; then
066  /bin/echo "lsregisterを削除してリセットします"
067  /bin/sync
068  sleep 1
069  /bin/sync
070  sleep 1
071  /usr/sbin/purge
072  sleep 1
073  "$STR_CMD_PATH" -delete
074  "$STR_CMD_PATH" -kill
075  /sbin/shutdown -r now
076elif [[ "$STR_USER_ANS" =~ ^[Nn]$ ]]; then
077  /bin/echo "処理を終了しますa"
078  exit 0
079else
080  /bin/echo "処理を終了しますb"
081  exit 1
082fi
AppleScriptで生成しました

|

launchctl 基本


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

#!/bin/bash
###
ITEM_UTI="com.foo.hoge"
/bin/echo "$ITEM_UTI"
STR_FILE_PATH="/Library/LaunchDaemons/$ITEM_UTI.plist"
if [ -f "$STR_FILE_PATH" ]; then
 /bin/launchctl stop -w "$STR_FILE_PATH"
 /bin/launchctl unload -w "$STR_FILE_PATH"
 /bin/launchctl disable -w "$STR_FILE_PATH"
 /bin/launchctl bootout -w "$STR_FILE_PATH"
 /bin/launchctl kill -w "$STR_FILE_PATH"
 /bin/rm "$STR_FILE_PATH"
fi

ITEM_UTI="com.foo.hoge"
/bin/echo "$ITEM_UTI"
STR_FILE_PATH="/Library/LaunchDaemons/$ITEM_UTI.plist"
if [ -f "$STR_FILE_PATH" ]; then
 /bin/launchctl start -w "$STR_FILE_PATH"
 /bin/launchctl load -w "$STR_FILE_PATH"
 /bin/launchctl enable -w "$STR_FILE_PATH"
 /bin/launchctl bootstrap -w "$STR_FILE_PATH"
 /bin/launchctl kickstart -w "$STR_FILE_PATH"
 /bin/rm "$STR_FILE_PATH"
fi


|

lsregister

コマンドは
lsregister
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister
データ
マスターはSQLでパスは
/var/folders/XX/XXXXXXXXXXXXXXXX/0/com.apple.LaunchServices.dv/
通常セキュリティ上『アクセス権が無い』特殊なファイル
自動生成されるので、不具合発生時には削除しても『まぁ』大丈夫



lsregisterはスポットライトのインデックスからも値を参照するので
再構築時には、『先に』スポットライトのインデックスの再構築が必要


ユーザードメインでは
SQLファイルが
$HOME/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2
PLISTが
$HOME/Library/Preferences/com.apple.LaunchServices にあります


lsregister: [OPTIONS] [ <path>... ]

                      [ -apps <domain>[,domain]... ]

                      [ -libs <domain>[,domain]... ]

                      [ -all  <domain>[,domain]... ]


Paths are searched for applications to register with the Launch Service database.

Valid domains are "system", "local", "network" and "user". Domains can also

be specified using only the first letter.


  -delete       Delete the Launch Services database file. You must then reboot!

  -kill         Reset the Launch Services database before doing anything else

  -seed         If database isn't seeded, scan default locations for applications and libraries to register

  -lint         Print information about plist errors while registering bundles

  -lazy n       Sleep for n seconds before registering/scanning

  -r            Recursive directory scan, do not recurse into packages or invisible directories

  -R            Recursive directory scan, descending into packages and invisible directories

  -f            force-update registration even if mod date is unchanged

  -u            unregister instead of register

  -v            Display progress information

  -gc           Garbage collect old data and compact the database

  -dump [table] Display full database contents after registration

  -h            Display this help


sh-3.2$ 

sh-3.2$ 

sh-3.2$ 

sh-3.2$ 

sh-3.2$ /usr/bin/sqlite3 $HOME/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV2

SQLite version 3.37.0 2021-12-09 01:34:53

Enter ".help" for usage hints.

sqlite> 

sqlite> 

sqlite> .tables

LSQuarantineEvent

sqlite> .mode column

sqlite> select * from LSQuarantineEvent;

LSQuarantineEventIdentifier           LSQuarantineTimeStamp  LSQuarantineAgentBundleIdentifier  LSQuarantineAgentName  LSQuarantineDataURLString    


この後ものすごい量のデータが出ます

                                                                                                                                                    

↑よく見ると、インストールされたアプリケーションの入手先URLがわかります。


mkdir -pm 700 $HOME/bin


ln -s /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister $HOME/bin/lsregister



$HOME/bin/lsregister -lint com.tinyspeck.slackmacgap


$HOME/bin/lsregister -delete -r -domain user

$HOME/bin/lsregister -delete -r -domain local

$HOME/bin/lsregister -delete -r -domain network

$HOME/bin/lsregister -delete -r -domain system


$HOME/bin/lsregister -kill -r -domain user


$HOME/bin/lsregister -kill -r -v -f -domain user

$HOME/bin/lsregister -kill -r -domain local

$HOME/bin/lsregister -kill -r -domain network

$HOME/bin/lsregister -kill -r -domain system



$HOME/bin/lsregister -seed  -r -v -f -domain user

$HOME/bin/lsregister -seed -r -domain local

$HOME/bin/lsregister -seed -r -domain network

$HOME/bin/lsregister -seed -r -domain system


$HOME/bin/lsregister -lint  -r -v -f -domain -apps  user

$HOME/bin/lsregister -lint -r -domain  -all local

$HOME/bin/lsregister -lint -r -domain  -all network

$HOME/bin/lsregister -lint -r -domain -all system



$HOME/bin/lsregister -seed  -r -v -f -domain  -apps user

$HOME/bin/lsregister -seed  -r -v -f -domain  -libs user

$HOME/bin/lsregister -seed  -r -v -f -domain  -all user



$HOME/bin/lsregister -dump  -r -v -f -domain  -apps user | grep uti:  

$HOME/bin/lsregister -dump  -r -v -f -domain  -apps user | grep "type id:"  

$HOME/bin/lsregister -dump  -r -v -f -domain  -apps user | grep "claim id:"  





|

[AppPath]アプリケーションを探す


###この方法だと起動しちゃう

set aliasAppPath to (path to application id "com.tinyspeck.slackmacgap")
log aliasAppPath
set strAppPath to POSIX path of (path to application id "com.tinyspeck.slackmacgap")
log strAppPath


tell application "Finder"
return POSIX path of (application file id "com.tinyspeck.slackmacgap" as text)
end tell





set strAppName to "Slack.app"

set strIsregisterPath to "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister" as text

set strResponse to (do shell script strIsregisterPath & " -dump | grep --only-matching \"/.*\\" & strAppName & "\"")

全てのアプリケーションを取得するなら

tell application "Finder" to set appNames to name of application files in entire contents of (path to applications folder)


tell application "Finder" to set appNames to name of application files in entire contents of (path to home folder)

こちらは書き方が違うだけ

set strApplicationFolderPath to (path to applications folder from local domain) as text
set strTextEditPath to strApplicationFolderPath & "TextEdit.app" as text



my posixPathTo:"TextEdit.app"

on posixPathTo:appName
set bundleID to id of application appName
tell application "Finder" to return POSIX path of (application file id bundleID as alias)
end posixPathTo:



use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

set ocidWorkSpace to current application's NSWorkspace's sharedWorkspace()

set ocidAppPath to ocidWorkSpace's URLForApplicationWithBundleIdentifier:"com.tinyspeck.slackmacgap"

set strAppFilePath to ocidAppPath's absoluteString() as text
log "strAppFilePath: " & strAppFilePath

set aliasAppPath to ocidAppPath as alias
log "aliasAppPath: " & aliasAppPath

set strAppPath to ocidAppPath's |path|() as text
log "strAppPath: " & strAppPath

|

その他のカテゴリー

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