Skip to content

Instantly share code, notes, and snippets.

@marxspawn
Last active August 14, 2018 13:49
Show Gist options
  • Save marxspawn/3c43958a04dfdcee1b01c099ca957264 to your computer and use it in GitHub Desktop.
Save marxspawn/3c43958a04dfdcee1b01c099ca957264 to your computer and use it in GitHub Desktop.
####################
####################
##
## This script was made so I could try out a few different AppleScript functions.
##
## Most of which is just re-hashed from different codes and examples found on the AppleScript Language Guide.
##
## https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html )
##
## I added multiple unecessary dialog boxes for exception/error handling, looped responses and such.
##
## I exported a finished/working script.app using Script Editor.
##
## Then added it to my login items in preferences to auto-load and mount at start-up
##
####################
####################
-- LoopCounter is a parent script to HighLoopCounter
-- Both contain optional response dialogs change up looped dialogs
script LoopCounter
property Cycles : 0
to giveAttempt to user
set Cycles to Cycles + 1
return display dialog "Try again? " & (Cycles - 5) * -1 & " remain" -- added cycle counter
end giveAttempt
end script
-- A Child of the LoopCounter script to add use for looped responses
script HighLoopCounter
property parent : LoopCounter
on giveAttempt to user
if my Cycles > 3 then
display dialog "TimeMachine was not mounted" -- Final loop display
else
continue giveAttempt to user -- Shows the parent dialog untill
end if
end giveAttempt
end script
--
## Create a boolean response to verify drive availability by checking the network SSID
set results to boolean
set desiredSSID to "DD-WRT" -- Assign the home networks SSID to compare with the current network SSID
set thisSSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/ SSID: / {print $2}'"
get thisSSID
## If/Else statement to determine weather to proceed or not
try
if thisSSID is not "DD-WRT" then
set results to false & (display dialog "SSID: " & thisSSID & "
Unidentified network connection " & "
Cancelling Process")
return {results, thisSSID, desiredSSID} -- Exit process and printout results
else if thisSSID is "DD-WRT" and desiredSSID is "DD-WRT" then
set results to true & (display dialog "SSID: " & thisSSID & "
Home Network Identified" & "
Continuing Process")
end if
end try
repeat (5) times
considering case
set prompt to "Enter Password to Mount TimeMachine.sparsebundle"
-- Setup the main dialog option with a generic password field hidden in prompt
-- Set variable for buttons
-- Label buttons -- Enable default selection
set buttonOption to display dialog prompt ¬
buttons {"Enter", "Exit"} default button 1 ¬
default answer "ADMIN1234" with hidden answer -- Input default Password with hidden input
set defaultPass to text returned of buttonOption -- set variable to recall the user input
if button returned of buttonOption is "Enter" and defaultPass is "ADMIN1234" or button returned of buttonOption is "Enter" and defaultPass is "Secure" then
mount volume "smb://192.168.0.144/volume2" --finish ip address and NAS drive location
-- first give mountpoint for the sparsebundle then provide location of the sparsebundle
do shell script ("hdiutil attach -mountpoint /Volumes/TimeMachine.sparsebundle/ /Volumes/location/OfImage/TimeMachine.sparsebundle")
exit repeat
-- Display a confirmation message
display dialog "TimeMachine Mounted"
end if
if button returned of buttonOption is "Enter" and defaultPass is not "ADMIN1234" or buttonOption is "Enter" and defaultPass is not "Secure" then
tell HighLoopCounter to giveAttempt to defaultPass
end if
if button returned of buttonOption = "Exit" then
set prompt to "Do you want to exit?"
-- Setup dialog options for password
set buttonOptionTwo to display dialog prompt ¬
buttons {"Cancel Exit", "Continue Exit"} default button 1 ¬
-- Assign variable recall user selection
set defaultPass to text returned of buttonOption
if button returned of buttonOptionTwo is "Continue Exit" then
return
else if button returned of buttonOptionTwo is "Cancel Exit" then
end if
end if
end considering
end repeat
return end
@marxspawn
Copy link
Author

Practice with Apples OOP AppleScript

@marxspawn
Copy link
Author

Added SSID boolean check

  • App will now end process if connected to different network other than the home network

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment