20260421

【Finder】デスクトップフォルダの内容の表示・非表示

【Finder】デスクトップフォルダの内容の表示・非表示

NOTE記事一覧ですnote.com

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

AppleShowAllFiles.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
004(*
005
006ショートカット(Shortcuts)用
007
008不可視ファイルの表示有無
009デスクトップの有無の変更
010
011com.cocolog-nifty.quicktimer.icefloe *)
012----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
013##自分環境がos12なので2.8にしているだけです
014use AppleScript version "2.8"
015use framework "Foundation"
016use scripting additions
017
018
019
020
021set strItem1 to ("【有】不可視ファイル") as text
022set strItem2 to ("【無】不可視ファイル") as text
023set strItem3 to ("【有】デスクトップ") as text
024set strItem4 to ("【無】デスクトップ") as text
025
026set listSelection to {strItem1, strItem2, strItem3, strItem4} as list
027set strOK to ("設定変更") as text
028set strCancel to ("キャンセル") as text
029set strTitle to ("処理を選んでください") as text
030set strPrompt to ("実行する処理を選んでください" & return & "Finderが再起動します") as text
031
032set strCmd to ("/usr/bin/defaults read com.apple.finder CreateDesktop") as text
033try
034   set numBool to (do shell script strCmd) as integer
035on error strErrMes number numErrNo
036   log strErrMes & numErrNo
037   set numBool to 1 as integer
038end try
039if numBool is 1 then
040   set numListNo to (last item of listSelection)
041else
042   set numListNo to (third item of listSelection)
043end if
044
045
046try
047   tell application "System Events"
048      activate
049      set valueResponse to (choose from list listSelection with title strTitle with prompt strPrompt default items numListNo OK button name strOK cancel button name strCancel with empty selection allowed without multiple selections allowed)
050   end tell
051on error strErrMes number numErrNo
052   log strErrMes & numErrNo
053   return false
054end try
055if (class of valueResponse) is boolean then
056   error "ユーザによってキャンセルされました。" number -128
057else if (class of valueResponse) is list then
058   if valueResponse is {} then
059      error "Error 何も選んでいません" number -128
060   else
061      set strResponse to (first item of valueResponse) as text
062   end if
063end if
064
065
066
067if strResponse is strItem1 then
068   set strCmd to ("/usr/bin/defaults write  com.apple.finder AppleShowAllFiles -bool true") as text
069else if strResponse is strItem2 then
070   set strCmd to ("/usr/bin/defaults write  com.apple.finder AppleShowAllFiles -bool false") as text
071else if strResponse is strItem3 then
072   set strCmd to ("/usr/bin/defaults write com.apple.finder CreateDesktop -bool true") as text
073else if strResponse is strItem4 then
074   set strCmd to ("/usr/bin/defaults write com.apple.finder CreateDesktop -bool false") as text
075end if
076
077try
078   do shell script strCmd
079on error strErrMes number numErrNo
080   log strErrMes & numErrNo
081   #エラーしたら 不可視無し デスクトップ有りに変更する
082   delay 1
083   set strCmd to ("/usr/bin/defaults write  com.apple.finder AppleShowAllFiles -bool false") as text
084   do shell script strCmd
085   delay 1
086   set strCmd to ("/usr/bin/defaults write com.apple.finder CreateDesktop -bool true") as text
087   do shell script strCmd
088end try
089
090# Finder再起動
091set strCmd to ("/usr/bin/killall Finder") as text
092
093
094try
095   do shell script strCmd
096on error strErrMes number numErrNo
097   log strErrMes & numErrNo
098   #エラーしたら通知
099   tell application "System Events"
100      activate
101      display alert "設定変更でエラーになりました"
102   end tell
103   return false
104end try
105
106return true
AppleScriptで生成しました