Help in optimizing a script


-- start var

try
	set FirstRun to (do shell script "defaults read com.apple.superchanger -first-run")
on error
	
	do shell script "defaults write com.apple.superchanger first-run -bool false"
end try
set FinderQuitOption to (do shell script "defaults read com.apple.finder QuitMenuItem")
set screenshotshadow to (do shell script "defaults read com.apple.screencapture disable-shadow")
set ShowFileEx to (do shell script "defaults read NSGlobalDomain AppleShowAllExtensions")
set HideDesktopIcons to (do shell script "defaults read com.apple.finder CreateDesktop")
set FirstRun to (do shell script "defaults read com.apple.superchanger first-run")

-- show Welcome message if not ran before
if FirstRun is "1" then --bug here
	display dialog "" buttons {""} default button 1
	do shell script "defaults write com.apple.superchanger first-run -bool true"
end if



set opfinder to {"Disable screenshot shadows on a Mac", "Adding ‘Quit’ option to Finder on a Mac", "Display the file extensions in Finder", "hide desktop items"}
set op to {"Finder"}
set selop to {choose from list op}

if selop is {{"Finder"}} then --done
	set sefinder to {choose from list opfinder}
	if sefinder is {{"Disable screenshot shadows on a Mac"}} then
		if "screenshotshadow" is "0" then
			do shell script "defaults write com.apple.screencapture disable-shadow -bool true; killall SystemUIServer"
		else
			do shell script "defaults write com.apple.screencapture disable-shadow -bool false; killall SystemUIServer"
		end if
		
	else if sefinder is {{"Adding ‘Quit’ option to Finder on a Mac"}} then --done
		if FinderQuitOption is "0" then
			do shell script "defaults write com.apple.finder QuitMenuItem -bool true; killall Finder"
		else
			do shell script "defaults write com.apple.finder QuitMenuItem -bool FALSE; killall Finder"
		end if
	else if sefinder is {{"Display the file extensions in Finder"}} then
		if ShowFileEx is "1" then
			do shell script "defaults write NSGlobalDomain AppleShowAllExtensions -bool FALSE; killall Finder"
		else
			do shell script "defaults write NSGlobalDomain AppleShowAllExtensions -bool TRUE; killall Finder"
		end if
	else if sefinder is {{"hide desktop items"}} then
		if HideDesktopIcons is "1" then
			do shell script "defaults write com.apple.finder CreateDesktop -bool FALSE; killall Finder"
		else
			do shell script "defaults write com.apple.finder CreateDesktop -bool TRUE; killall Finder"
		end if
	end if
	
else if selop is {{"Dock"}} then
	-- item 2 action goes here
end if

It has a bug which i do not know how to fix and it is way to big

AppleScript: 2.10
Browser: Firefox 95.0
Operating System: macOS 10.13

Hi test123testa. A belated welcome to MacScripter!

MacScripter’s Code Exchange forum is for sharing working scripts or techniques that you’ve written (or know about) which you think may be of interest to others. The forum for asking for help with AppleScript is AppleScript | Mac OS X. I’ve asked the site’s owner to move your previous topic there, but so far this hasn’t happened. :confused:

-- start var

try
	set FirstRun to (do shell script "defaults read com.apple.superchanger first-run")
on error the error_message number the error_number
	do shell script "defaults write com.apple.superchanger first-run -bool false "
end try


--do shell script "defaults write com.apple.superchanger first-run -bool false"
--end try
repeat
	set FinderQuitOption to (do shell script "defaults read com.apple.finder QuitMenuItem")
	set screenshotshadow to (do shell script "defaults read com.apple.screencapture disable-shadow")
	set ShowFileEx to (do shell script "defaults read NSGlobalDomain AppleShowAllExtensions")
	set HideDesktopIcons to (do shell script "defaults read com.apple.finder CreateDesktop")
	set FirstRun to (do shell script "defaults read com.apple.superchanger first-run")
	
	-- show Welcome message if not ran before
	if FirstRun is "0" then
		display dialog "Welcome to Superchanger!" buttons {"OK"} default button 1 with icon 1
		do shell script "defaults write com.apple.superchanger first-run -bool true"
	end if
	
	
	
	set opfinder to {"Disable screenshot shadows on a Mac", "Adding ‘Quit’ option to Finder on a Mac", "Display the file extensions in Finder", "hide desktop items"}
	set op to {"Finder", "Quit"}
	set selop to {choose from list op}
	
	if selop is {{"Finder"}} then --done
		set sefinder to {choose from list opfinder}
		if sefinder is {{"Disable screenshot shadows on a Mac"}} then
			if "screenshotshadow" is "0" then
				do shell script "defaults write com.apple.screencapture disable-shadow -bool true; killall SystemUIServer"
			else
				do shell script "defaults write com.apple.screencapture disable-shadow -bool false; killall SystemUIServer"
			end if
			
		else if sefinder is {{"Adding ‘Quit’ option to Finder on a Mac"}} then --done
			if FinderQuitOption is "0" then
				do shell script "defaults write com.apple.finder QuitMenuItem -bool true; killall Finder"
			else
				do shell script "defaults write com.apple.finder QuitMenuItem -bool FALSE; killall Finder"
			end if
		else if sefinder is {{"Display the file extensions in Finder"}} then
			if ShowFileEx is "1" then
				do shell script "defaults write NSGlobalDomain AppleShowAllExtensions -bool FALSE; killall Finder"
			else
				do shell script "defaults write NSGlobalDomain AppleShowAllExtensions -bool TRUE; killall Finder"
			end if
		else if sefinder is {{"hide desktop items"}} then
			if HideDesktopIcons is "1" then
				do shell script "defaults write com.apple.finder CreateDesktop -bool FALSE; killall Finder"
			else
				do shell script "defaults write com.apple.finder CreateDesktop -bool TRUE; killall Finder"
			end if
		end if
		
	else if selop is {{"Dock"}} then
		-- item 2 action goes here
	else if selop is {{"Quit"}} then
		quit
	end if
	
end repeat


















This is way to big. I need help.

AppleScript: 2.10
Browser: Firefox 95.0
Operating System: macOS 10.13

A very bad approach is used in your script. A lot of unnecessary and it is not clear why the repeat loop. I think, you’re not going to change the settings every few seconds along with endless dialogs popping up…

I would use the following simple script as it is (or make it a service in the Automator):


set aChoice to choose from list {"Disable screenshot shadows on a Mac", "Display the file extensions in Finder", "Hide desktop items"}

if aChoice is false then return
set aChoice to item 1 of aChoice

if aChoice is "Display the file extensions in Finder" then
	tell application "Finder" to tell Finder preferences to set all name extensions showing to true
else if aChoice is "Hide desktop items" then
	tell application "Finder" to quit it
else -- it is "Disable screenshot shadows on a Mac"
	do shell script "defaults write com.apple.screencapture disable-shadow true;killall SystemUIServer"
end if

First you should not be using “apple” in your domain.
Create one that reflects your own “com..”

Secondly you don’t really offer folks to view the current state of a property.
And the ability to turn it on/off. It only seemed like you could turn it on?

You might want to look into using something like Shane Dialog ToolKit

https://latenightsw.com/freeware/
There some good examples to help you out.
You can create ONE interface that will show what current states are and let the use
change them (via radio buttons). And then you can write all the new state.

Rather than having multiple Choose From keep popping up.

As I noted above, you can use the simple script in post #2 as is, or make it a service.

Now, I also want to show an advanced approach in terms of convenience. I think the user who asked for help should like it. Also, I do not mind that the authorship belongs to @test123testa, as the author of the idea:


-- application "SuperChanger"
-- save and use it as Stay-Open Application (important)
-- no need third-party solutions
-- to rechange some settings, you need only to click application's icon in the Dock

on run -- this is executed only on first run 
	display dialog "Welcome to SuperChanger!" buttons {"OK"} default button 1 with icon 1
	my changeSetting()
end run

on reopen -- this is executed when clicking app's icon in the Dock 
	my changeSetting()
end reopen

on changeSetting()
	set aChoice to choose from list {"Enable screenshot shadows", "Disable screenshot shadows", "", "Display file extensions", "Hide file extensions", "", "Show Finder items", "Hide Finder items"}
	if aChoice is false then return
	set aChoice to item 1 of aChoice
	
	if aChoice is "" then
		return
	else if aChoice is "Hide file extensions" then
		tell application "Finder" to tell Finder preferences to set all name extensions showing to false
		display alert "File extensions is hidden now"
	else if aChoice is "Display file extensions" then
		tell application "Finder" to tell Finder preferences to set all name extensions showing to true
		display alert "File extensions is visible now"
	else if aChoice is "Show Finder items" then
		tell application "Finder" to if not running then activate it
		activate me
		display alert "Finder items is visible now"
	else if aChoice is "Hide Finder items" then
		tell application "Finder" to if running then quit it
		display alert "Finder items is hidden now"
	else if aChoice is "Enable screenshot shadows" then
		do shell script "defaults write com.apple.screencapture disable-shadow false;killall SystemUIServer"
		display alert "Screenshot shadows is enabled now"
	else -- it is "Disable screenshot shadows"
		do shell script "defaults write com.apple.screencapture disable-shadow true;killall SystemUIServer"
		display alert "Screenshot shadows is disabled now"
	end if
end changeSetting