content: -- By: Pico Mitchell
  -- For: MacLand @ Free Geek
  --
  -- MIT License
  --
  -- Copyright (c) 2021 Free Geek
  --
  -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  -- to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  -- and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  --
  -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  --
  -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  -- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  --
  
  -- Version: 2022.2.22-1
  
  -- App Icon is “Optical Disc” from Twemoji (https://twemoji.twitter.com/) by Twitter (https://twitter.com)
  -- Licensed under CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
  
  -- Build Flag: LSUIElement
  
  use AppleScript version "2.7"
  use scripting additions
  
  repeat -- dialogs timeout when screen is asleep or locked (just in case)
  	set isAwake to true
  	try
  		set isAwake to ((do shell script ("bash -c " & (quoted form of "/usr/libexec/PlistBuddy -c 'Print :0:IOPowerManagement:CurrentPowerState' /dev/stdin <<< \"$(ioreg -arc IODisplayWrangler -k IOPowerManagement -d 1)\""))) is equal to "4")
  	end try
  	
  	set isUnlocked to true
  	try
  		set isUnlocked to ((do shell script ("bash -c " & (quoted form of "/usr/libexec/PlistBuddy -c 'Print :IOConsoleUsers:0:CGSSessionScreenIsLocked' /dev/stdin <<< \"$(ioreg -ac IORegistryEntry -k IOConsoleUsers -d 1)\""))) is not equal to "true")
  	end try
  	
  	if (isAwake and isUnlocked) then
  		exit repeat
  	else
  		delay 1
  	end if
  end repeat
  
  try
  	set infoPlistPath to ((POSIX path of (path to me)) & "Contents/Info.plist")
  	((infoPlistPath as POSIX file) as alias)
  	
  	set intendedAppName to "Test CD" -- Hardcode intended App name because Name or Bundle Identifier changes should not be done lightly or accidentally.
  	
  	try
  		do shell script ("/usr/libexec/PlistBuddy -c 'Print :FGBuiltByMacLandScriptBuilder' " & (quoted form of infoPlistPath))
  		((((POSIX path of (path to me)) & "Contents/MacOS/" & intendedAppName) as POSIX file) as alias)
  	on error
  		try
  			activate
  		end try
  		display alert "
  “" & (name of me) & "” must be built by the “MacLand Script Builder” script." buttons {"Quit"} default button 1 as critical
  		quit
  		delay 10
  	end try
  	
  	set AppleScript's text item delimiters to "-"
  	set intendedBundleIdentifier to ("org.freegeek." & ((words of intendedAppName) as string))
  	set currentBundleIdentifier to ((do shell script ("/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' " & (quoted form of infoPlistPath))) as string)
  	if (currentBundleIdentifier is not equal to intendedBundleIdentifier) then error "“" & (name of me) & "” does not have the correct Bundle Identifier.
  
  
  Current Bundle Identifier:
  	" & currentBundleIdentifier & "
  
  Intended Bundle Identifier:
  	" & intendedBundleIdentifier
  on error checkInfoPlistError
  	if (checkInfoPlistError does not start with "Can’t make file") then
  		try
  			activate
  		end try
  		display alert checkInfoPlistError buttons {"Quit"} default button 1 as critical
  		quit
  		delay 10
  	end if
  end try
  
  try
  	set mainScptPath to ((POSIX path of (path to me)) & "Contents/Resources/Scripts/main.scpt")
  	((mainScptPath as POSIX file) as alias)
  	do shell script "osadecompile " & (quoted form of mainScptPath)
  	error "
  “" & (name of me) & "” must be exported as a Run-Only Script."
  on error checkReadOnlyErrorMessage
  	if ((checkReadOnlyErrorMessage does not contain "errOSASourceNotAvailable") and (checkReadOnlyErrorMessage does not start with "Can’t make file")) then
  		activate
  		display alert checkReadOnlyErrorMessage buttons {"Quit"} default button 1 as critical
  		quit
  		delay 10
  	end if
  end try
  
  
  set dialogIconName to "applet"
  try
  	((((POSIX path of (path to me)) & "Contents/Resources/" & (name of me) & ".icns") as POSIX file) as alias)
  	set dialogIconName to (name of me)
  end try
  
  set systemVersion to (system version of (system info))
  considering numeric strings
  	set isMojaveOrNewer to (systemVersion ≥ "10.14")
  	set isCatalinaOrNewer to (systemVersion ≥ "10.15")
  end considering
  
  
  set adminUsername to "Staff"
  if (isCatalinaOrNewer) then set adminUsername to "staff"
  set adminPassword to "[MACLAND SCRIPT BUILDER WILL REPLACE THIS PLACEHOLDER WITH OBFUSCATED ADMIN PASSWORD]"
  
  set buildInfoPath to ((POSIX path of (path to shared documents folder)) & "Build Info/")
  try
  	(((buildInfoPath & ".fgSetupSkipped") as POSIX file) as alias)
  	
  	try
  		do shell script ("mkdir " & (quoted form of buildInfoPath))
  	end try
  	try
  		set AppleScript's text item delimiters to "-"
  		do shell script ("touch " & (quoted form of (buildInfoPath & ".fgLaunchAfterSetup-org.freegeek." & ((words of (name of me)) as string)))) user name adminUsername password adminPassword with administrator privileges
  	end try
  	
  	try
  		-- For some reason, on Big Sur, apps are not opening unless we specify "-n" to "Open a new instance of the application(s) even if one is already running." All scripts have LSMultipleInstancesProhibited to this will not actually ever open a new instance.
  		do shell script "open -n -a '/Applications/Test Boot Setup.app'"
  	end try
  	
  	quit
  	delay 10
  end try
  
  
  set iTunesOrMusic to "iTunes"
  if (isCatalinaOrNewer) then set iTunesOrMusic to "Music"
  
  if (isMojaveOrNewer) then
  	try
  		tell application "System Events" to every window -- To prompt for Automation access on Mojave
  	on error automationAccessErrorMessage number automationAccessErrorNumber
  		if (automationAccessErrorNumber is equal to -1743) then
  			try
  				tell application "System Preferences"
  					try
  						activate
  					end try
  					reveal ((anchor "Privacy") of (pane id "com.apple.preference.security"))
  				end tell
  			end try
  			try
  				activate
  			end try
  			try
  				display dialog "“" & (name of me) & "” must be allowed to control and perform actions in “System Events” to be able to function.
  
  
  USE THE FOLLOWING STEPS TO FIX THIS ISSUE:
  
  • Open the “System Preferences” application.
  
  • Click the “Security & Privacy” preference pane.
  
  • Select the “Privacy” tab.
  
  • Select “Automation” in the source list on the left.
  
  • Find “" & (name of me) & "” in the list on the right and turn on the “System Events” checkbox underneath it.
  
  • Relaunch “" & (name of me) & "” (using the button below)." buttons {"Quit", "Relaunch “" & (name of me) & "”"} cancel button 1 default button 2 with title (name of me) with icon dialogIconName
  				try
  					do shell script "osascript -e 'delay 0.5' -e 'repeat while (application \"" & (POSIX path of (path to me)) & "\" is running)' -e 'delay 0.5' -e 'end repeat' -e 'do shell script \"open -n -a \\\"" & (POSIX path of (path to me)) & "\\\"\"' > /dev/null 2>&1 &"
  				end try
  			end try
  			quit
  			delay 10
  		end if
  	end try
  end if
  
  try
  	tell application "System Events" to tell application process "Finder" to (get windows)
  on error (assistiveAccessTestErrorMessage)
  	if ((offset of "not allowed assistive" in assistiveAccessTestErrorMessage) > 0) then
  		if (isMojaveOrNewer) then
  			try
  				tell application iTunesOrMusic to every window -- To prompt for Automation access on Mojave
  			on error automationAccessErrorMessage number automationAccessErrorNumber
  				if (automationAccessErrorNumber is equal to -1743) then
  					try
  						tell application "System Preferences"
  							try
  								activate
  							end try
  							reveal ((anchor "Privacy") of (pane id "com.apple.preference.security"))
  						end tell
  					end try
  					try
  						activate
  					end try
  					try
  						display dialog "“" & (name of me) & "” must be allowed to control and perform actions in “" & iTunesOrMusic & "” to be able to function.
  
  
  USE THE FOLLOWING STEPS TO FIX THIS ISSUE:
  
  • Open the “System Preferences” application.
  
  • Click the “Security & Privacy” preference pane.
  
  • Select the “Privacy” tab.
  
  • Select “Automation” in the source list on the left.
  
  • Find “" & (name of me) & "” in the list on the right and turn on the “" & iTunesOrMusic & "” checkbox underneath it.
  
  • Relaunch “" & (name of me) & "” (using the button below)." buttons {"Quit", "Relaunch “" & (name of me) & "”"} cancel button 1 default button 2 with title (name of me) with icon dialogIconName
  						try
  							do shell script "osascript -e 'delay 0.5' -e 'repeat while (application \"" & (POSIX path of (path to me)) & "\" is running)' -e 'delay 0.5' -e 'end repeat' -e 'do shell script \"open -n -a \\\"" & (POSIX path of (path to me)) & "\\\"\"' > /dev/null 2>&1 &"
  						end try
  					end try
  					quit
  					delay 10
  				end if
  			end try
  			tell application iTunesOrMusic to quit
  		end if
  		
  		try
  			tell application "Finder" to reveal (path to me)
  		end try
  		try
  			tell application "System Preferences"
  				try
  					activate
  				end try
  				reveal ((anchor "Privacy_Accessibility") of (pane id "com.apple.preference.security"))
  			end tell
  		end try
  		try
  			activate
  		end try
  		try
  			display dialog "“" & (name of me) & "” must be allowed to control this computer using Accessibility Features to be able to function.
  
  
  USE THE FOLLOWING STEPS TO FIX THIS ISSUE:
  
  • Open the “System Preferences” application.
  
  • Click the “Security & Privacy” preference pane.
  
  • Select the “Privacy” tab.
  
  • Select “Accessibility” in the source list on the left.
  
  • Click the Lock icon at the bottom left of the window, enter the administrator username and password, and then click Unlock.
  
  • Find “" & (name of me) & "” in the list on the right and turn on the checkbox next to it. If “" & (name of me) & "” IS NOT in the list, drag-and-drop the app icon from Finder into the list.
  
  • Relaunch “" & (name of me) & "” (using the button below)." buttons {"Quit", "Relaunch “" & (name of me) & "”"} cancel button 1 default button 2 with title (name of me) with icon dialogIconName
  			try
  				do shell script "osascript -e 'delay 0.5' -e 'repeat while (application \"" & (POSIX path of (path to me)) & "\" is running)' -e 'delay 0.5' -e 'end repeat' -e 'do shell script \"open -n -a \\\"" & (POSIX path of (path to me)) & "\\\"\"' > /dev/null 2>&1 &"
  			end try
  		end try
  		quit
  		delay 10
  	end if
  end try
  
  
  try
  	with timeout of 1 second
  		tell application "DVD Player" to quit
  	end timeout
  end try
  
  try
  	set volume output volume 75 without output muted
  end try
  try
  	set volume alert volume 100
  end try
  
  -- Since target may be "iTunes" or "Music", scripting must be done with "run script" to be compiled on the fly
  -- since compiling when targeting a variable will error because of not knowing the correct scripting dictionary.
  
  try
  	run script "
  		if (application \"" & iTunesOrMusic & "\" is running) then tell application \"" & iTunesOrMusic & "\" to set visible of browser window 1 to true
  		
  		tell application \"" & iTunesOrMusic & "\"
  			try
  				activate
  			end try
  			set sound volume to 80
  		end tell
  	"
  end try
  
  delay 5 -- Wait for possible CD Lookup Results window to open.
  
  try -- Mute volume so we don't beep in case CD Lookup Results are not visible.
  	set volume output volume 0 with output muted
  end try
  try
  	set volume alert volume 0
  end try
  
  try
  	tell application "System Events" to tell application process iTunesOrMusic
  		set frontmost to true
  		keystroke return -- To close possible CD Lookup Results window (which doesn't show up when getting windows with AppleScript).
  	end tell
  end try
  
  delay 0.5
  
  try
  	set volume output volume 75 without output muted
  end try
  try
  	set volume alert volume 100
  end try
  
  try
  	tell application "System Events" to tell application process iTunesOrMusic
  		set frontmost to true
  		repeat with thisButton in (buttons of window 1)
  			if ((name of thisButton) is "No Thanks") then
  				set frontmost to true
  				click thisButton
  				exit repeat
  			end if
  		end repeat
  	end tell
  end try
  
  delay 0.5
  
  try
  	run script "
  		tell application \"" & iTunesOrMusic & "\"
  			try
  				activate
  			end try
  			set cdPlaylist to (playlist 1 of (first source whose kind is audio CD))
  			reveal cdPlaylist
  			delay 0.5
  			play cdPlaylist
  		end tell
  	"
  end try
  