Tuesday, July 17, 2018

#1 2014-11-24 09:42:15 pm

wontonunist
Member
Registered: 2011-06-21
Posts: 27

Show or Hide Invisible Files Utility - Feedback wanted

Tested on OS X 10.8. Thoughts?

Applescript:

set {_prompts, _buttons} to {{_yes:"Files are visible.", _no:"Files are hidden."}, {_yes:"Hide Files", _no:"Show Files"}}

repeat
   
   set visibility to (do shell script "defaults read com.apple.finder AppleShowAllFiles")
   
   if visibility is "YES" then
       set _prompt to _yes of _prompts
       set _button to _yes of _buttons
   else if visibility is "NO" then
       set _prompt to _no of _prompts
       set _button to _no of _buttons
   end if
   
   activate me
   
   set action to button returned of (display dialog _prompt buttons {"Quit", _button} with title "Show/Hide Files" cancel button 1 default button 2 with icon 2)
   
   if action is "Hide Files" then
       do shell script "defaults write com.apple.finder AppleShowAllFiles NO"
   else if action is "Show Files" then
       do shell script "defaults write com.apple.finder AppleShowAllFiles YES"
   end if
   
   display dialog "Restart Finder?" with title "Show/Hide Files" buttons {"No", "Yes"} default button 2
   if button returned of result is "No" then exit repeat
   
   tell application "Finder" to quit
   display dialog "Finder restarting..." with title "Show/Hide Files" buttons {""} giving up after 2
   
   if gave up of result is false then
       delay 2
   else
       tell application "Finder" to activate
   end if
   
end repeat

Model: MacBook Pro 15"
AppleScript: AppleScript 2.3.1
Browser: Safari 537.74.9
Operating System: Mac OS X (10.8)

Offline

 

#2 2014-11-25 12:09:17 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11499
Website

Re: Show or Hide Invisible Files Utility - Feedback wanted

Hi,

the key AppleShowAllFiles returns a boolean value which is passed thru the shell as "0' or "1".
To get a real boolean value you have to write

Applescript:


   set visibility to ((do shell script "defaults read com.apple.finder AppleShowAllFiles") as integer) as boolean

then you can check

Applescript:


   if visibility then
       set _prompt to _yes of _prompts
       set _button to _yes of _buttons
   else
       set _prompt to _no of _prompts
       set _button to _no of _buttons
   end if

to set the value the proper syntax is

Applescript:


do shell script "defaults write com.apple.finder AppleShowAllFiles -bool NO" -- or YES

I'm using this to just toggle the state of AppleShowAllFiles and restart the Finder

Applescript:


try
   set state to ((do shell script "/usr/bin/defaults read com.apple.finder AppleShowAllFiles") as integer) as boolean
on error
   set state to false
end try
do shell script "/usr/bin/defaults write com.apple.finder AppleShowAllFiles -bool " & ((not state) as text) & "; killall Finder"


regards

Stefan

Offline

 

#3 2014-12-04 03:19:38 pm

partron22
Member
Registered: 2011-03-18
Posts: 86

Re: Show or Hide Invisible Files Utility - Feedback wanted

If you want to choose, rather than toggle:

Applescript:

set buttonpressed to button returned of (display dialog "Show Hidden Files?" buttons {"Yes", "No"})

try
   if the buttonpressed is "No" then do shell script "defaults write com.apple.finder AppleShowAllFiles OFF"
   if the buttonpressed is "Yes" then do shell script "defaults write com.apple.finder AppleShowAllFiles ON"
   do shell script "killall Finder"
end try

Offline

 

#4 2014-12-05 03:21:31 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 545

Re: Show or Hide Invisible Files Utility - Feedback wanted

No need to coerce text to integer to boolean:

Applescript:

"yes" as boolean
--> true

Offline

 

#5 2014-12-05 05:38:12 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2776
Website

Re: Show or Hide Invisible Files Utility - Feedback wanted

alastor933 wrote:

No need to coerce text to integer to boolean:

Applescript:

"yes" as boolean
--> true


I'm not sure what you excactly mean but the returned results of a boolean in shell is either "0" or "1" and not "YES" or "NO". That is only how you are setting the data. StefanK's code is correct, a direct as boolean will return in an error.

Applescript:


"1" as boolean
--> error

Last edited by DJ Bazzie Wazzie (2014-12-05 05:38:37 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)