AppleScript Visual Studio Code

[Code]設定変更(画像をバイナリーエディタで開く)

[Visual Studio Code]settings.jsonを開く
https://quicktimer.cocolog-nifty.com/icefloe/2023/04/post-412f43.html
202402180331141188x678

        "image/*": "hexeditor"

|

[Visual Studio Code]ファイルを開く(自分用)

コピペ用に自分が使っているやつ

ダウンロード - openfilecode.zip


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

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

property refMe : a reference to current application
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()

set strFileName to "コピペ用.applescript"

tell application "Finder"
  set aliasPathToMe to (path to me) as alias
  set aliasContainerPathToMe to container of aliasPathToMe as alias
  set aliasFilePath to (file strFileName of folder "doc" of folder aliasContainerPathToMe) as alias
end tell

set strFilePaht to POSIX path of aliasFilePath as text

set ocidFilePathURL to refMe's NSURL's fileURLWithPath:strFilePaht
set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
ocidFilePathURLArray's insertObject:ocidFilePathURL atIndex:0

set ocidOpenConfiguration to refMe's NSWorkspaceOpenConfiguration's configuration()
ocidOpenConfiguration's setActivates:(true as boolean)
ocidOpenConfiguration's setHides:(false as boolean)
ocidOpenConfiguration's setRequiresUniversalLinks:(false as boolean)
ocidOpenConfiguration's setCreatesNewApplicationInstance:(true as boolean)

set strBundleID to "com.microsoft.VSCode"
set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID)
if ocidAppBundle is not (missing value) then
  set ocidAppBundlePath to ocidAppBundle's bundlePath()
  set ocidAppPath to ocidAppBundlePath's stringByStandardizingPath
  set ocidAppPathURL to refMe's NSURL's fileURLWithPath:(ocidAppPath)
else
  set ocidAppPathURL to appShardWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)
end if

try
  ## バージョン 1.83までの方法
  ## set strCommandText to "/usr/bin/open \"vscode://file/" & strFilePaht & "\"" as text
  ## バージョン1.84から
  set strCommandText to "/usr/bin/open -b com.microsoft.VSCode \"" & strFilePaht & "\"" as text
  do shell script strCommandText
  log "Openコマンドで開きました"
  
on error
  try
    tell application id "com.microsoft.VSCode"
      open file aliasFilePath
    end tell
  on error
appShardWorkspace's openURLs:ocidFilePathURLArray withApplicationAtURL:ocidAppPathURL configuration:ocidOpenConfiguration completionHandler:(missing value)
    log "NSWorkspaceで開きました"
  end try
end try


return

|

[VSCode]VSCodeのバイナリーのパス

Screen-20231029-153953

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

#!/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 strBundleID to ("com.microsoft.VSCode") as text


####インストール先を調べる
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID))
if ocidAppBundle ≠ (missing value) then
  set ocidAppPathURL to ocidAppBundle's bundleURL()
else if ocidAppBundle = (missing value) then
  set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
end if
if ocidAppPathURL = (missing value) then
  tell application "Finder"
    try
      set aliasAppApth to (application file id strBundleID) as alias
    on error
return "アプリケーションが見つかりませんでした"
    end try
  end tell
  set strAppPath to POSIX path of aliasAppApth as text
  set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
  set strAppPath to strAppPathStr's stringByStandardizingPath()
  set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:true
end if
###バイナリーのパス
set ocidBinPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Resources/app/bin/code")
set strBinPathURL to (ocidBinPathURL's |path|()) as text
###コマンド実行
set strCommandText to ("\"" & strBinPathURL & "\" --help") as text
do shell script strCommandText


return strBinPathURL

|

[Visual Studio Code]HISTORY削除


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

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

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()


####################################
### History
set listDirPath to {"~/Library/Application Support/Code/User/History", "~/Library/Application Support/Code/logs", "~/Library/Application Support/Code/Cache", "~/Library/Application Support/Code/CachedData", "~/Library/Application Support/Code/CachedExtensionVSIXs", "~/Library/Application Support/Code/Crashpad", "~/Library/Application Support/Code/Service Worker/CacheStorage"} as list

repeat with itemDirPath in listDirPath
  ##パス
  set strPath to itemDirPath as text
  set ocidFilePathStr to (refMe's NSString's stringWithString:(strPath))
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:true)
  ###コンテンツ収集
  set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
  set listDirContantsArray to (appFileManager's contentsOfDirectoryAtURL:(ocidFilePathURL) includingPropertiesForKeys:({(refMe's NSURLPathKey)}) options:(ocidOption) |error|:(reference))
  set ocidContentsPathArray to (item 1 of listDirContantsArray)
  ###収集したコンテンツをゴミ箱へ移動
  repeat with itemContentsPathArray in ocidContentsPathArray
    set listDone to (appFileManager's trashItemAtURL:(itemContentsPathArray) resultingItemURL:(itemContentsPathArray) |error|:(reference))
    log item 1 of listDone
  end repeat
  
end repeat



|

[VS CODE]vs code のHTMLコピー


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

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

##########################################
###ペーストボード
##########################################
####ペーストボード宣言
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
####中に格納されているデータタイプを取得
set ocidPastBoardTypeArray to ocidPasteboard's types

######HTMLを受け取る
if (ocidPastBoardTypeArray's containsObject:"public.html") is true then
  set ocidPublicHTML to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeHTML)
else
  display alert "内容をコピーしてから実行してね(Visual Studio Code専用です。)"
return "内容をコピーしてから実行してね(Visual Studio Code専用です。)"
end if

log ocidPublicHTML as text

set ocidPublicHTML to doRegrExReplace(ocidPublicHTML, "\n", "")
set ocidPublicHTML to doRegrExReplace(ocidPublicHTML, "<meta charset='utf-8'>", "")
##置換済みHTMLをテキスト形式に
set strPublicHTML to ocidPublicHTML as text
##############################
#####ダイアログ
##############################

set strPublicHTML to (strPublicHTML & "\n")

set aliasIconPath to POSIX file "/System/Applications/TextEdit.app/Contents/Resources/AppIcon.icns" as alias

tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
  end tell
else
  tell current application
    activate
  end tell
end if
try
  set strMes to "戻り値です" as text
  set recordResult to (display dialog strMes with title strMes default answer strPublicHTML buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer) as record
on error
  log "エラーしました"
end try
if (gave up of recordResult) is true then
return "時間切れです"
end if
##############################
#####値のコピー
##############################
if button returned of recordResult is "クリップボードにコピー" then
  try
    set strText to text returned of recordResult as text
    ####ペーストボード宣言
    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
    set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
  on error
    tell application "Finder"
      set the clipboard to strText as text
    end tell
  end try
end if


##########################################
####HTMLの置換 正規表現
##########################################
to doRegrExReplace(argText, argReplaceString, argWithString)
  set ocidReturnText to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidReturnText's setString:argText
  ###レンンジを取って(元データの元データのレンジ=最初から最後まで)
  set ocidArgTextRange to ocidReturnText's rangeOfString:ocidReturnText
  ###置換
ocidReturnText's replaceOccurrencesOfString:(argReplaceString) withString:(argWithString) options:(refMe's NSRegularExpressionSearch) range:ocidArgTextRange
return ocidReturnText
end doRegrExReplace


|

[VsCode] テンプレートを使う


ダウンロード - templates.zip


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

#!/bin/bash
#
#新規BASHのテンプレート
############################################
STR_DATE=$(/bin/date '+%s')
/bin/echo "$STR_DATE"
STR_FILE_PATH="$HOME/Desktop/$STR_DATE.bash"
/usr/bin/touch "$STR_FILE_PATH"
/bin/chmod 755 "$STR_FILE_PATH"
###このファイル
SCRIPT_PATH="${BASH_SOURCE[0]}"
/bin/echo "実行したファイル: $SCRIPT_PATH"
###コンテナ
STR_CONTAINER_DIR_PATH=$(/usr/bin/dirname "$SCRIPT_PATH")
/bin/echo "コンテナ: $STR_CONTAINER_DIR_PATH"
###ファイル名
STR_FILE_NAME="template.txt"
/bin/echo "ファイル名: $STR_FILE_NAME"
###テンプレートファイル※ここで指定したファイルを新規で開く
STR_TMP_FILE_PATH="$STR_CONTAINER_DIR_PATH""/$STR_FILE_NAME"
/bin/echo "テンプレートファイル: $STR_TMP_FILE_PATH"
###ファイルを作って
/usr/bin/touch "$STR_FILE_PATH"
###内容をコピーする
/bin/echo "$(/bin/cat "$STR_TMP_FILE_PATH")" >> "$STR_FILE_PATH"

###########
#できたファイルをVS CODEで開く
exec /usr/bin/osascript - "$STR_FILE_PATH" <<EOF
property theFilePath : ""
on run theFilePath
set aliasFilePath to posix file theFilePath as alias
tell application "System Events"
tell application id "com.microsoft.VSCode"
open theFilePath
end tell
end tell
end run
EOF


###########
exit 0;




|

Visual Studio Code で書類を開く


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# 選択肢はVSCodeのCFBundleDocumentTypesから取得する方法
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()


set strBundleID to "com.microsoft.VSCode"

###################################
#####対象アプリの対応ContentTypesを取得
###################################
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID)
if ocidAppBundle is not (missing value) then
  set ocidAppPathURL to ocidAppBundle's bundleURL()
else
  set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace()
  set ocidAppPathURL to appNSWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)
end if
set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
#####CFBundleDocumentTypesの取得
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
set ocidDocTypeArray to ocidPlistDict's objectForKey:"CFBundleDocumentTypes"
####リストにする
set listUTl to {} as list
repeat with itemDocTypeArray in ocidDocTypeArray
  set listContentTypes to (itemDocTypeArray's objectForKey:"LSItemContentTypes")
  if listContentTypes = (missing value) then
    set ocidExtensionArray to (itemDocTypeArray's objectForKey:"CFBundleTypeExtensions")
    repeat with itemExtensionArray in ocidExtensionArray
      set ocidContentTypes to (refMe's UTType's typeWithFilenameExtension:(itemExtensionArray))
      set strContentTypes to ocidContentTypes's identifier() as text
      set strContentTypes to ("" & strContentTypes & "") as text
      set end of listUTl to (strContentTypes)
    end repeat
  else
    repeat with itemContentTypes in listContentTypes
      set strContentTypes to ("" & itemContentTypes & "") as text
      set end of listUTl to (strContentTypes)
    end repeat
  end if
end repeat

###################################
#####入力ダイアログ
###################################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set ocidUserDesktopPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserDesktopPathURL to ocidUserDesktopPathURLArray's firstObject()
set aliasDefaultLocation to (ocidUserDesktopPathURL's absoluteURL()) as alias
### set listUTI to {"public.png", "public.jpeg"}
set strPromptText to "ファイルを選んでください" as text
set strMesText to "ファイルを選んでください" as text
##
set listAliasFilePath to (choose file strMesText with prompt strPromptText default location (aliasDefaultLocation) of type listUTl with invisibles and showing package contents without multiple selections allowed) as list

set aliasFilePath to (item 1 of listAliasFilePath) as alias
set strFilePath to (POSIX path of aliasFilePath) as text


set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(strFilePath)
set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
ocidFilePathURLArray's insertObject:ocidFilePathURL atIndex:0

set ocidOpenConfiguration to refMe's NSWorkspaceOpenConfiguration's configuration()
ocidOpenConfiguration's setActivates:(true as boolean)
ocidOpenConfiguration's setHides:(false as boolean)
ocidOpenConfiguration's setRequiresUniversalLinks:(false as boolean)
ocidOpenConfiguration's setCreatesNewApplicationInstance:(true as boolean)


set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID)
if ocidAppBundle is not (missing value) then
  set ocidAppBundlePath to ocidAppBundle's bundlePath()
  set ocidAppPath to ocidAppBundlePath's stringByStandardizingPath
  set ocidAppPathURL to refNSURL's fileURLWithPath:(strAppPath)
else
  set ocidAppPathURL to appShardWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)
end if

try
  set strCommandText to "/usr/bin/open \"vscode://file/" & strFilePaht & "\"" as text
  do shell script strCommandText
  log "Openコマンドで開きました"
on error
appShardWorkspace's openURLs:(ocidFilePathURLArray) withApplicationAtURL:(ocidAppPathURL) configuration:(ocidOpenConfiguration) completionHandler:(missing value)
  log "NSWorkspaceで開きました"
end try

|

[Ms Vs Code]アップデート

ダウンロード - mscodezipinstall.zip


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

#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
########################################
##OS
PLIST_PATH="/System/Library/CoreServices/SystemVersion.plist"
STR_OS_VER=$(/usr/bin/defaults read "$PLIST_PATH" ProductVersion)
/bin/echo "OS VERSION :" "$STR_OS_VER"
STR_MAJOR_VERSION="${STR_OS_VER%%.*}"
/bin/echo "STR_MAJOR_VERSION :" "$STR_MAJOR_VERSION"
STR_MINOR_VERSION="${STR_OS_VER#*.}"
/bin/echo "STR_MINOR_VERSION :" "$STR_MINOR_VERSION"

########################################
##ユーザー
USER_WHOAMI=$(/usr/bin/whoami)
/bin/echo "実行したユーザーは:$USER_WHOAMI"
CURRENT_USER=$(/bin/echo "$HOME" | /usr/bin/awk -F'/' '{print $NF}')
/bin/echo "実行ユーザー:" "$CURRENT_USER"

########################################
##デバイス
USER_TEMP_DIR=$(/usr/bin/mktemp -d)
/bin/echo "起動時に削除されるディレクトリ:" "$USER_TEMP_DIR"
#起動ディスクの名前を取得する
/usr/bin/touch "$USER_TEMP_DIR/diskutil.plist"
/usr/sbin/diskutil info -plist / >"$USER_TEMP_DIR/diskutil.plist"
STARTUPDISK_NAME=$(/usr/bin/defaults read "$USER_TEMP_DIR/diskutil.plist" VolumeName)
/bin/echo "ボリューム名:" "$STARTUPDISK_NAME"
########################################
###ダウンロード起動時に削除する項目

###CPUタイプでの分岐
ARCHITEC=$(/usr/bin/arch)
/bin/echo "Running on $ARCHITEC"
if [ "$ARCHITEC" == "arm64" ]; then
  ###ARM用のダウンロードURL
  STR_URL="https://code.visualstudio.com/sha/download?build=stable&os=darwin-arm64"
  ###ファイル名を取得
  DL_FILE_NAME=$(/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' "$STR_URL" | /usr/bin/rev | /usr/bin/cut -d'/' -f1 | /usr/bin/rev)
  /bin/echo "DL_FILE_NAME:$DL_FILE_NAME"
  ###ダウンロード
  if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/$DL_FILE_NAME" "$STR_URL" --connect-timeout 20; then
    /bin/echo "ファイルのダウンロードに失敗しました HTTP1.1で再トライします"
    if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/$DL_FILE_NAME" "$STR_URL" --http1.1 --connect-timeout 20; then
      /bin/echo "ファイルのダウンロードに失敗しました"
      exit 1
    fi
  fi
else
  ###INTEL用のダウンロードURL
  STR_URL="https://code.visualstudio.com/sha/download?build=stable&os=darwin"
  ###ファイル名を取得
  DL_FILE_NAME=$(/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' "$STR_URL" | /usr/bin/rev | /usr/bin/cut -d'/' -f1 | /usr/bin/rev)
  /bin/echo "DL_FILE_NAME:$DL_FILE_NAME"
  ###ダウンロード
  if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/$DL_FILE_NAME" "$STR_URL" --connect-timeout 20; then
    /bin/echo "ファイルのダウンロードに失敗しました HTTP1.1で再トライします"
    if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/$DL_FILE_NAME" "$STR_URL" --http1.1 --connect-timeout 20; then
      /bin/echo "ファイルのダウンロードに失敗しました"
      exit 1
    fi
  fi
fi
##実行可能にしておく
/bin/chmod 755 "$USER_TEMP_DIR/$DL_FILE_NAME"
/bin/echo "ダウンロードOK"

########################################
###アプリケーションの終了
###コンソールユーザーにのみ処理する
CONSOLE_USER=$(/bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }')
/bin/echo "CONSOLE_USER:$CONSOLE_USER"
if [ -z "$CONSOLE_USER" ]; then
  /bin/echo "コンソールユーザーが無い=電源入れてログインウィンドウ状態"
else
  #####OSAスクリプトはエラーすることも多い(初回インストール時はエラーになる)
  if ! /usr/bin/osascript -e "tell application id \"com.microsoft.VSCode\" to quit"; then
    ##念の為 KILLもする
    /usr/bin/killall "Code" 2>/dev/null
    /usr/bin/killall "Code Helper" 2>/dev/null
    /usr/bin/killall "Code Helper (GPU)" 2>/dev/null
    /usr/bin/killall "Code Helper (Renderer)" 2>/dev/null
  fi
fi
/bin/sleep 2

############################################################
##基本メンテナンス

##ライブラリの不可視属性を解除
/usr/bin/chflags nohidden "/Users/$CURRENT_USER/Library"
/usr/bin/SetFile -a v "/Users/$CURRENT_USER/Library"
##ユーザーアプリケーションフォルダを作る
/bin/mkdir -p "/Users/$CURRENT_USER/Applications"
/bin/chmod 700 "/Users/$CURRENT_USER/Applications"
/usr/sbin/chown "$CURRENT_USER" "/Users/$CURRENT_USER/Applications"
/usr/bin/touch "/Users/$CURRENT_USER/Applications/.localized"
##ユーザーユーティリティフォルダを作る
/bin/mkdir -p "/Users/$CURRENT_USER/Applications/Utilities"
/bin/chmod 755 "/Users/$CURRENT_USER/Applications/Utilities"
/usr/sbin/chown "$CURRENT_USER" "/Users/$CURRENT_USER/Applications/Utilities"
/usr/bin/touch "/Users/$CURRENT_USER/Applications/Utilities/.localized"
## Managed Itemsフォルダを作る
/bin/mkdir -p "/Users/$CURRENT_USER/Library/Managed Items"
/bin/chmod 755 "/Users/$CURRENT_USER/Library/Managed Items"
/usr/sbin/chown "$CURRENT_USER" "/Users/$CURRENT_USER/Library/Managed Items"
/usr/bin/touch "/Users/$CURRENT_USER/Library/Managed Items/.localized"
##アクセス権チェック
/bin/chmod 700 "/Users/$CURRENT_USER/Library"
/bin/chmod 700 "/Users/$CURRENT_USER/Movies"
/bin/chmod 700 /"Users/$CURRENT_USER/Music"
/bin/chmod 700 "/Users/$CURRENT_USER/Pictures"
/bin/chmod 700 "/Users/$CURRENT_USER/Downloads"
/bin/chmod 700 "/Users/$CURRENT_USER/Documents"
/bin/chmod 700 "/Users/$CURRENT_USER/Desktop"
##全ローカルユーザーに対して実施したい処理があれば追加する
/bin/echo "ユーザーディレクトリチェックDONE"

############################################################
#####古いファイルをゴミ箱に USER
function DO_MOVE_TO_TRASH() {
  if [ -e "$1" ]; then
    TRASH_DIR=$(/usr/bin/mktemp -d "/Users/$CURRENT_USER/.Trash/CODE.XXXXXXXX")
    /bin/chmod 777 "$TRASH_DIR"
    /bin/mv "$1" "$TRASH_DIR"
  fi
}

#####古いファイルをゴミ箱に
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Applications/Visual Studio Code.app"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Cache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedConfigurations"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedData"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedExtensions"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedExtensionVSIXs"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedProfilesData"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Code Cache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Crashpad"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/DawnCache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/GPUCache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/logs"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Service Worker/CacheStorage"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Service Worker/ScriptCache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/vscode-beggar"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Caches/com.microsoft.VSCode"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Caches/com.microsoft.VSCode.ShipIt"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/HTTPStorages/com.microsoft.VSCode"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Saved Application State/com.microsoft.VSCode.savedState"

/bin/echo "ユーザーキャッシュ削除DONE"
############################################################
###/var/folde T
MKTEMP_DIR=$(/usr/bin/mktemp -d)
TEMP_DIR_T="$(/usr/bin/dirname "$MKTEMP_DIR")"
function DO_MOVE_TO_TRASH_T() {
  if [ -e "$1" ]; then
    TRASH_DIR=$(/usr/bin/mktemp -d "/Users/$CURRENT_USER/.Trash/CODE.T.XXXXXXXX")
    /bin/chmod 777 "$TRASH_DIR"
    /bin/mv "$1" "$TRASH_DIR"
  fi
}
#####古いファイルをゴミ箱に
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper (GPU)"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper (Renderer)"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper-(GPU)"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper-(Renderer)"



# ディレクトリを再帰的に検索し、パターンに一致するものを処理する
TMP_T_TYPESCRIPT_DIR=$(/bin/ls "$TEMP_DIR_T" | /usr/bin/grep "vscode-typescript")
IFS=$'\n'
read -d '\n' -r -a LIST_TYPESCRIPT_DIR <<<"$TMP_T_TYPESCRIPT_DIR"
IFS=''
for ITEM_LIST_TYPESCRIPT_DIR in "${LIST_TYPESCRIPT_DIR[@]}"; do
  /bin/echo "$ITEM_LIST_TYPESCRIPT_DIR"
  /bin/mv "$TEMP_DIR_T/$ITEM_LIST_TYPESCRIPT_DIR" "$TRASH_DIR"
done

# ディレクトリを再帰的に検索し、パターンに一致するものを処理する
TMP_T_TYPESCRIPT_DIR=$(/bin/ls "$TEMP_DIR_T" | /usr/bin/grep "vscode-git")
IFS=$'\n'
read -d '\n' -r -a LIST_TYPESCRIPT_DIR <<<"$TMP_T_TYPESCRIPT_DIR"
IFS=''
for ITEM_LIST_TYPESCRIPT_DIR in "${LIST_TYPESCRIPT_DIR[@]}"; do
  /bin/echo "$ITEM_LIST_TYPESCRIPT_DIR"
  /bin/mv "$TEMP_DIR_T/$ITEM_LIST_TYPESCRIPT_DIR" "$TRASH_DIR"
done





/bin/echo "ユーザーキャッシュT DONE"

############################################################
###/var/folder C
TEMP_DIR="$(/usr/bin/dirname "$TEMP_DIR_T")"
TEMP_DIR_C="${TEMP_DIR}/C"
function DO_MOVE_TO_TRASH_C() {
  if [ -e "$1" ]; then
    TRASH_DIR=$(/usr/bin/mktemp -d "/Users/$CURRENT_USER/.Trash/CODE.C.XXXXXXXX")
    /bin/chmod 777 "$TRASH_DIR"
    /bin/mv "$1" "$TRASH_DIR"
  fi
}
#####古いファイルをゴミ箱に
DO_MOVE_TO_TRASH_C "$TEMP_DIR_C/com.microsoft.VSCode"
DO_MOVE_TO_TRASH_C "$TEMP_DIR_C/com.microsoft.VSCode.Helper"
DO_MOVE_TO_TRASH_C "$TEMP_DIR_C/com.microsoft.VSCode.Helper-(GPU)"
DO_MOVE_TO_TRASH_C "$TEMP_DIR_C/com.microsoft.VSCode.Helper-(Renderer)"
/bin/echo "ユーザーキャッシュ C DONE"

/usr/bin/unzip -q "$USER_TEMP_DIR/$DL_FILE_NAME" -d "/Users/$CURRENT_USER/Applications"

/bin/echo "LIST_USER:DOCK" "$CURRENT_USER"
PLIST_DICT="<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Users/$CURRENT_USER/Applications/Visual Studio Code.app/</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
/usr/bin/defaults write com.apple.dock persistent-apps -array-add "$PLIST_DICT"

###反映させて
/usr/bin/killall cfprefsd
###Dockを再起動
/usr/bin/killall Dock

/bin/echo "処理終了しました"

exit 0


|

[Bash]VScodeユーザーアップデート

ダウンロード - vscodezipinstall.zip


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

#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
########################################
##OS
PLIST_PATH="/System/Library/CoreServices/SystemVersion.plist"
STR_OS_VER=$(/usr/bin/defaults read "$PLIST_PATH" ProductVersion)
/bin/echo "OS VERSION :" "$STR_OS_VER"
STR_MAJOR_VERSION="${STR_OS_VER%%.*}"
/bin/echo "STR_MAJOR_VERSION :" "$STR_MAJOR_VERSION"
STR_MINOR_VERSION="${STR_OS_VER#*.}"
/bin/echo "STR_MINOR_VERSION :" "$STR_MINOR_VERSION"

########################################
##ユーザー
USER_WHOAMI=$(/usr/bin/whoami)
/bin/echo "実行したユーザーは:$USER_WHOAMI"
CURRENT_USER=$(/bin/echo "$HOME" | /usr/bin/awk -F'/' '{print $NF}')
/bin/echo "実行ユーザー:" "$CURRENT_USER"

########################################
##デバイス
USER_TEMP_DIR=$(/usr/bin/mktemp -d)
/bin/echo "起動時に削除されるディレクトリ:" "$USER_TEMP_DIR"
#起動ディスクの名前を取得する
/usr/bin/touch "$USER_TEMP_DIR/diskutil.plist"
/usr/sbin/diskutil info -plist / >"$USER_TEMP_DIR/diskutil.plist"
STARTUPDISK_NAME=$(/usr/bin/defaults read "$USER_TEMP_DIR/diskutil.plist" VolumeName)
/bin/echo "ボリューム名:" "$STARTUPDISK_NAME"
########################################
###ダウンロード起動時に削除する項目

###CPUタイプでの分岐
ARCHITEC=$(/usr/bin/arch)
/bin/echo "Running on $ARCHITEC"
if [ "$ARCHITEC" == "arm64" ]; then
  ###ARM用のダウンロードURL
  STR_URL="https://go.microsoft.com/fwlink/?linkid=2156837"
  ###ファイル名を取得
  DL_FILE_NAME=$(/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' "$STR_URL" | /usr/bin/rev | /usr/bin/cut -d'/' -f1 | /usr/bin/rev)
  /bin/echo "DL_FILE_NAME:$DL_FILE_NAME"
  ###ダウンロード
  if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/$DL_FILE_NAME" "$STR_URL" --connect-timeout 20; then
    /bin/echo "ファイルのダウンロードに失敗しました HTTP1.1で再トライします"
    if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/$DL_FILE_NAME" "$STR_URL" --http1.1 --connect-timeout 20; then
      /bin/echo "ファイルのダウンロードに失敗しました"
      exit 1
    fi
  fi
else
  ###INTEL用のダウンロードURL
  STR_URL="https://go.microsoft.com/fwlink/?linkid=2156837"
  ###ファイル名を取得
  DL_FILE_NAME=$(/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' "$STR_URL" | /usr/bin/rev | /usr/bin/cut -d'/' -f1 | /usr/bin/rev)
  /bin/echo "DL_FILE_NAME:$DL_FILE_NAME"
  ###ダウンロード
  if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/$DL_FILE_NAME" "$STR_URL" --connect-timeout 20; then
    /bin/echo "ファイルのダウンロードに失敗しました HTTP1.1で再トライします"
    if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/$DL_FILE_NAME" "$STR_URL" --http1.1 --connect-timeout 20; then
      /bin/echo "ファイルのダウンロードに失敗しました"
      exit 1
    fi
  fi
fi
##実行可能にしておく
/bin/chmod 755 "$USER_TEMP_DIR/$DL_FILE_NAME"
/bin/echo "ダウンロードOK"

########################################
###アプリケーションの終了
###コンソールユーザーにのみ処理する
CONSOLE_USER=$(/bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }')
/bin/echo "CONSOLE_USER:$CONSOLE_USER"
if [ -z "$CONSOLE_USER" ]; then
  /bin/echo "コンソールユーザーが無い=電源入れてログインウィンドウ状態"
else
  #####OSAスクリプトはエラーすることも多い(初回インストール時はエラーになる)
  if ! /usr/bin/osascript -e "tell application id \"com.microsoft.VSCode\" to quit"; then
    ##念の為 KILLもする
    /usr/bin/killall "Code" 2>/dev/null
    /usr/bin/killall "Code Helper" 2>/dev/null
    /usr/bin/killall "Code Helper (GPU)" 2>/dev/null
    /usr/bin/killall "Code Helper (Renderer)" 2>/dev/null
  fi
fi
/bin/sleep 2

############################################################
##基本メンテナンス

##ライブラリの不可視属性を解除
/usr/bin/chflags nohidden "/Users/$CURRENT_USER/Library"
/usr/bin/SetFile -a v "/Users/$CURRENT_USER/Library"
##ユーザーアプリケーションフォルダを作る
/bin/mkdir -p "/Users/$CURRENT_USER/Applications"
/bin/chmod 700 "/Users/$CURRENT_USER/Applications"
/usr/sbin/chown "$CURRENT_USER" "/Users/$CURRENT_USER/Applications"
/usr/bin/touch "/Users/$CURRENT_USER/Applications/.localized"
##ユーザーユーティリティフォルダを作る
/bin/mkdir -p "/Users/$CURRENT_USER/Applications/Utilities"
/bin/chmod 755 "/Users/$CURRENT_USER/Applications/Utilities"
/usr/sbin/chown "$CURRENT_USER" "/Users/$CURRENT_USER/Applications/Utilities"
/usr/bin/touch "/Users/$CURRENT_USER/Applications/Utilities/.localized"
## Managed Itemsフォルダを作る
/bin/mkdir -p "/Users/$CURRENT_USER/Library/Managed Items"
/bin/chmod 755 "/Users/$CURRENT_USER/Library/Managed Items"
/usr/sbin/chown "$CURRENT_USER" "/Users/$CURRENT_USER/Library/Managed Items"
/usr/bin/touch "/Users/$CURRENT_USER/Library/Managed Items/.localized"
##アクセス権チェック
/bin/chmod 700 "/Users/$CURRENT_USER/Library"
/bin/chmod 700 "/Users/$CURRENT_USER/Movies"
/bin/chmod 700 /"Users/$CURRENT_USER/Music"
/bin/chmod 700 "/Users/$CURRENT_USER/Pictures"
/bin/chmod 700 "/Users/$CURRENT_USER/Downloads"
/bin/chmod 700 "/Users/$CURRENT_USER/Documents"
/bin/chmod 700 "/Users/$CURRENT_USER/Desktop"
##全ローカルユーザーに対して実施したい処理があれば追加する
/bin/echo "ユーザーディレクトリチェックDONE"

############################################################
#####古いファイルをゴミ箱に USER
function DO_MOVE_TO_TRASH() {
  if [ -e "$1" ]; then
    TRASH_DIR=$(/usr/bin/mktemp -d "/Users/$CURRENT_USER/.Trash/CODE.XXXXXXXX")
    /bin/chmod 777 "$TRASH_DIR"
    /bin/mv "$1" "$TRASH_DIR"
  fi
}

#####古いファイルをゴミ箱に
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Applications/Visual Studio Code.app"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Cache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedConfigurations"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedData"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedExtensions"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedExtensionVSIXs"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/CachedProfilesData"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Code Cache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Crashpad"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/DawnCache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/GPUCache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/logs"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Service Worker/CacheStorage"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/Code/Service Worker/ScriptCache"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Application Support/vscode-beggar"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Caches/com.microsoft.VSCode"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Caches/com.microsoft.VSCode.ShipIt"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/HTTPStorages/com.microsoft.VSCode"
DO_MOVE_TO_TRASH "/Users/$CURRENT_USER/Library/Saved Application State/com.microsoft.VSCode.savedState"

/bin/echo "ユーザーキャッシュ削除DONE"
############################################################
###/var/folde T
MKTEMP_DIR=$(/usr/bin/mktemp -d)
TEMP_DIR_T="$(/usr/bin/dirname "$MKTEMP_DIR")"
function DO_MOVE_TO_TRASH_T() {
  if [ -e "$1" ]; then
    TRASH_DIR=$(/usr/bin/mktemp -d "/Users/$CURRENT_USER/.Trash/CODE.T.XXXXXXXX")
    /bin/chmod 777 "$TRASH_DIR"
    /bin/mv "$1" "$TRASH_DIR"
  fi
}
#####古いファイルをゴミ箱に
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper (GPU)"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper (Renderer)"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper-(GPU)"
DO_MOVE_TO_TRASH_T "$TEMP_DIR_T/Visual Studio Code Helper-(Renderer)"



# ディレクトリを再帰的に検索し、パターンに一致するものを処理する
TMP_T_TYPESCRIPT_DIR=$(/bin/ls "$TEMP_DIR_T" | /usr/bin/grep "vscode-typescript")
IFS=$'\n'
read -d '\n' -r -a LIST_TYPESCRIPT_DIR <<<"$TMP_T_TYPESCRIPT_DIR"
IFS=''
for ITEM_LIST_TYPESCRIPT_DIR in "${LIST_TYPESCRIPT_DIR[@]}"; do
  /bin/echo "$ITEM_LIST_TYPESCRIPT_DIR"
  /bin/mv "$TEMP_DIR_T/$ITEM_LIST_TYPESCRIPT_DIR" "$TRASH_DIR"
done

# ディレクトリを再帰的に検索し、パターンに一致するものを処理する
TMP_T_TYPESCRIPT_DIR=$(/bin/ls "$TEMP_DIR_T" | /usr/bin/grep "vscode-git")
IFS=$'\n'
read -d '\n' -r -a LIST_TYPESCRIPT_DIR <<<"$TMP_T_TYPESCRIPT_DIR"
IFS=''
for ITEM_LIST_TYPESCRIPT_DIR in "${LIST_TYPESCRIPT_DIR[@]}"; do
  /bin/echo "$ITEM_LIST_TYPESCRIPT_DIR"
  /bin/mv "$TEMP_DIR_T/$ITEM_LIST_TYPESCRIPT_DIR" "$TRASH_DIR"
done





/bin/echo "ユーザーキャッシュT DONE"

############################################################
###/var/folder C
TEMP_DIR="$(/usr/bin/dirname "$TEMP_DIR_T")"
TEMP_DIR_C="${TEMP_DIR}/C"
function DO_MOVE_TO_TRASH_C() {
  if [ -e "$1" ]; then
    TRASH_DIR=$(/usr/bin/mktemp -d "/Users/$CURRENT_USER/.Trash/CODE.C.XXXXXXXX")
    /bin/chmod 777 "$TRASH_DIR"
    /bin/mv "$1" "$TRASH_DIR"
  fi
}
#####古いファイルをゴミ箱に
DO_MOVE_TO_TRASH_C "$TEMP_DIR_C/com.microsoft.VSCode"
DO_MOVE_TO_TRASH_C "$TEMP_DIR_C/com.microsoft.VSCode.Helper"
DO_MOVE_TO_TRASH_C "$TEMP_DIR_C/com.microsoft.VSCode.Helper-(GPU)"
DO_MOVE_TO_TRASH_C "$TEMP_DIR_C/com.microsoft.VSCode.Helper-(Renderer)"
/bin/echo "ユーザーキャッシュ C DONE"

/usr/bin/unzip -q "$USER_TEMP_DIR/$DL_FILE_NAME" -d "/Users/$CURRENT_USER/Applications"

/bin/echo "LIST_USER:DOCK" "$CURRENT_USER"
PLIST_DICT="<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Users/$CURRENT_USER/Applications/Visual Studio Code.app/</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
/usr/bin/defaults write com.apple.dock persistent-apps -array-add "$PLIST_DICT"

###反映させて
/usr/bin/killall cfprefsd
###Dockを再起動
/usr/bin/killall Dock

/bin/echo "処理終了しました"

exit 0


|

[Visual Studio Code]HTMLコピー(自分用 BLOG投稿用)修正 $のパーセントエンコードを追加

ダウンロード - copycode.applescript.zip

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# 20230610 修正 $のパーセントエンコードを追加
#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 ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
####中に格納されているデータタイプを取得
set ocidPastBoardTypeArray to ocidPasteboard's types

######HTMLを受け取る
if (ocidPastBoardTypeArray's containsObject:"public.html") is true then
  set ocidPublicHTML to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeHTML)
else
return "Visual Studio Code専用です"
end if
#######コピーボタン用にプレインテキストを受け取る
if (ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text") is true then
  set ocidPublicText to ocidPasteboard's stringForType:"public.utf8-plain-text"
else if (ocidPastBoardTypeArray's containsObject:"NSPasteboardTypeString") is true then
  set ocidPublicText to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString)
end if
####HTML置換メタタグ除去
set ocidPublicText to doRegrExReplace(ocidPublicText, "<meta charset='utf-8'>", "")


##########################################
######コピーボタン用 text
##########################################
###置換レコード
set recordEntityMap to {|&|:"&amp;", |<|:"&lt;", |>|:"&gt;", |"|:"&quot;", |'|:"&apos;", |=|:"&#x3D;", |+|:"&#x2B;", |\\|:"&bsol;", |$|:"&#36;"} as record
###ディクショナリにして
set ocidEntityMap to refMe's NSDictionary's alloc()'s initWithDictionary:recordEntityMap
###キーの一覧を取り出します
set ocidAllKeys to ocidEntityMap's allKeys()

###可変テキストにして
set ocidTextToEncode to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidTextToEncode's setString:(ocidPublicText)

###取り出したキー一覧を順番に処理
repeat with itemAllKey in ocidAllKeys
  ##キーの値を取り出して
  set ocidMapValue to (ocidEntityMap's valueForKey:itemAllKey)
  ##置換
  set ocidEncodedText to (ocidTextToEncode's stringByReplacingOccurrencesOfString:(itemAllKey) withString:(ocidMapValue))
  ##次の変換に備える
  set ocidTextToEncode to ocidEncodedText
end repeat


####テキスト確定させて
set strEncodedText to ocidEncodedText as text
###HTMLのエレメントID用のランダム番号3桁
set num3Digit to random number from 100 to 999
###JAVAASCRIPT整形
set strJsText to "<script>const elmentButtonCopy" & num3Digit & " = document.getElementById('buttonCopy" & num3Digit & "');const elmentInputText" & num3Digit & " = document.getElementById('inputText" & num3Digit & "');elmentButtonCopy" & num3Digit & ".addEventListener('click', () => {const strInputTextValue = elmentInputText" & num3Digit & ".value;return navigator.clipboard.writeText(strInputTextValue);})</script>\n"
###HTML整形
set strCopyHTML to "<textarea id=\"inputText" & num3Digit & "\" type=\"text\" hidden readonly>" & strEncodedText & "</textarea>"
###出来上がり
set strCopyHTML to strCopyHTML & "<button id=\"buttonCopy" & num3Digit & "\">クリップボードへコピー</button>" & strJsText


##########################################
######リンクボタン AppleScript
##########################################
######## %エンコードする
##キャラクタセットを指定
set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
###ペーストボードの内容をキャラクタセットで変換
set ocidTextEncodeAS to ocidPublicText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
###可変テキストに格納しておく
set ocidEncodedText to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidEncodedText's setString:(ocidTextEncodeAS)

######## 置換 %エンコードの追加処理
###置換レコード
set recordPercentMap to {|+|:"%2B", |=|:"%3D", |&|:"%26", |$|:"%24"} as record
###ディクショナリにして
set ocidPercentMap to refMe's NSDictionary's alloc()'s initWithDictionary:recordPercentMap
###キーの一覧を取り出します
set ocidAllKeys to ocidPercentMap's allKeys()

###取り出したキー一覧を順番に処理
repeat with itemAllKey in ocidAllKeys
  ##キーの値を取り出して
  set ocidMapValue to (ocidPercentMap's valueForKey:itemAllKey)
  ##置換
  set ocidEncodedText to (ocidEncodedText's stringByReplacingOccurrencesOfString:(itemAllKey) withString:(ocidMapValue))
  ##次の変換に備える
  set ocidTextToEncode to ocidEncodedText
end repeat

set strEncodedText to ocidTextToEncode as text


################################
######HTML生成
################################
set strAsLinkText to "<hr><p class=\"openscript\"><a href=\"applescript://com.apple.scripteditor?action=new&name=com.cocolog-nifty.quicktimer&script=" & strEncodedText & "\" title=\"Open in Script Edito\">【スクリプトエディタで開く】</a>| " & strCopyHTML & "</p>" as text

##########################
###起動時に削除するフォルダ
##########################
###ファイルマネージャー初期化
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidFilePathURL to appFileManager's temporaryDirectory()
set ocidCleanupAtStartupURL to ocidFilePathURL's URLByAppendingPathComponent:"Cleanup At Startup" isDirectory:true
####アクセス権777で作成
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
appFileManager's createDirectoryAtURL:ocidCleanupAtStartupURL withIntermediateDirectories:true attributes:ocidAttrDict |error|:(reference)


##########################
###出力ファイル名とパス
##########################
###中間ファイルの名前は全部日付
set strTime to doGetDateNo("yyyyMMddhhmmss") as text
####ファイル名
set strHtmlFileName to ("" & strTime & ".html") as text
set strTextFileName to ("" & strTime & ".txt") as text
###パスURL
set ocidFilePathURL to ocidCleanupAtStartupURL's URLByAppendingPathComponent:(strHtmlFileName) isDirectory:false
set ocidTextFilePathURL to ocidCleanupAtStartupURL's URLByAppendingPathComponent:(strTextFileName) isDirectory:false

##########################################
####HTMLデータを読み込み
##########################################
###可変テキストに
set ocidHTMLData to refMe's NSMutableString's alloc()'s initWithCapacity:0
###セット
ocidHTMLData's setString:ocidPublicHTML

##########################################
####HTMLの置換 正規表現
##########################################
##CSS調整用
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "padding: 5px;", "padding: 5px;")
###他
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "\n|\r", "")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "line-height: 18px", "line-height: 18px")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "<meta charset='utf-8'>", "<div class=\"sourceCode\">")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, ">$", "></div>")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "\t", "&nbsp;&nbsp;&nbsp;&nbsp;")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "\\u00A0\\u0020", "&nbsp;&nbsp;")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "\\u00A0", "&nbsp;")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "> </span>", ">&nbsp;</span>")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "> </span>", ">&nbsp;&nbsp;</span>")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "> </span>", ">&nbsp;&nbsp;&nbsp;</span>")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "> </span>", ">&nbsp;&nbsp;&nbsp;&nbsp;</span>")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "> </span>", ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "> </span>", ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "> </span>", ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>")
set ocidHTMLData to doRegrExReplace(ocidHTMLData, "font-size: 16px", "font-size: 16px")

######リンク追加
###レンンジを取って
set ocidNSRange to ocidHTMLData's rangeOfString:ocidHTMLData
###置換
ocidHTMLData's replaceOccurrencesOfString:"^" withString:strAsLinkText options:(refMe's NSRegularExpressionSearch) range:ocidNSRange
#######それ以外の置換
###レンンジを取って
set ocidNSRange to ocidHTMLData's rangeOfString:ocidHTMLData
###置換
ocidHTMLData's replaceOccurrencesOfString:"$" withString:"<hr>" options:(refMe's NSRegularExpressionSearch) range:ocidNSRange


##########################################
####保存 と テキストファイルのコピー
##########################################
ocidHTMLData's writeToURL:(ocidFilePathURL) atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)

###拡張子をtxtにしたソース閲覧用のファイルを作る
appFileManager's copyItemAtURL:(ocidFilePathURL) toURL:(ocidTextFilePathURL) |error|:(reference)

##########################################
####最初に作ったTEXTファイル
##########################################

set aliasFilePath to (ocidTextFilePathURL's absoluteURL()) as alias

###ソーステキストで開く
tell application "TextEdit"
  open (aliasFilePath as alias)
  tell front document to activate
end tell




##########################################
####HTMLの置換 正規表現
##########################################
to doRegrExReplace(argText, argReplaceString, argWithString)
  set ocidReturnText to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidReturnText's setString:argText
  ###レンンジを取って(元データの元データのレンジ=最初から最後まで)
  set ocidArgTextRange to ocidReturnText's rangeOfString:ocidReturnText
  ###置換
ocidReturnText's replaceOccurrencesOfString:(argReplaceString) withString:(argWithString) options:(refMe's NSRegularExpressionSearch) range:ocidArgTextRange
return ocidReturnText
end doRegrExReplace


##########################################
####日付情報の取得
##########################################
to doGetDateNo(strDateFormat)
  ####日付情報の取得
  set ocidDate to refMe's NSDate's |date|()
  ###日付のフォーマットを定義
  set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"en_US")
ocidNSDateFormatter's setDateFormat:strDateFormat
  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
  set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo

|

その他のカテゴリー

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