I have this script that tells you what the start-up disk is, as there are several disks that the computer can start up from and when people are using it that are not aware of it, I want to alert them. FYI there are several disks attached to the Mac.
Script is:
with timeout of 3600 seconds -- set to 1 hour maximum = 3600
delay 5
tell application "System Events"
activate
set visible of every process whose visible is true and name is not "Finder" to false
end tell
--
tell application "Finder" to display alert " Starting up from: ===> " & (get name of startup disk) & "
" message "
===> Please VARIFY!
Is this indeed the Start-up disk you want to use!
If not restart after selecting the correct disk.
Your current start-up disk is: ===> " & (get name of startup disk)
--
tell application "Finder"
if name of startup disk contains "BU" then display alert "PAY ATTENTION!.
You are using the Back-Up Start-up disk, namely:
----> " & (get name of startup disk) & " <----
Are you sure or do you need to restart on the other disk?"
end tell
delay 1
tell application "Finder"
activate
set visible of every process whose visible is false and name is not "Finder" to true
delay 2
end tell
end timeout -- If x hours isn't enough, use a larger number.
However, it does not run properly, and sometimes it crashes.
The other thing that happens is that the alert window hides behind open folders on the desktop or is not visible at all when there are no open windows on the desktop. FYI, I work with 3 different desktops. If you move to the next desktop, or simply click on the desktop, the alert window becomes visible.
It has been fine for years but since I moved to 10.15 it started to behave not as it once did.
I’m using 10.12 so maybe this does nothing for you but what happens if you use System Events for the bulk of the operation (rather than the Finder)? I get the impression that Apple keeps deprecating some AS/Finder functionality with every system release.
tell application "Finder" to set visible of every process whose visible is true and name is not "Finder" to false
tell application "System Events"
activate
set sDisk to name of startup disk as text
if sDisk contains "BU" then
set da to display alert "Is " & sDisk & " the correct startup disk?" message "You are working from the 'backup' disk" as critical buttons {"Oops", "Yes"} cancel button {"Oops"}
end if
end tell
By ordering it this way, it displays the alert regardless of which desktop I launch it from and the alert always comes to the fore. To test, I set up two desktops, each of which had an open Finder window. You can then use either the Finder or System Events to unhide everything.
I don’t think that displaying foreground alerts has anything to do with Finder. It is enough not to hide the front window of the current application (this script), and not the Finder, as you both do. Either don’t hide anything, or hide everything except the window of the current application (this script). I would not hide anything:
tell application "System Events" to set sDisk to name of startup disk as text
if sDisk contains "BU" then ¬
display alert "Is " & sDisk & ¬
" the correct startup disk?" message "You are working from the 'backup' disk" as critical ¬
buttons {"Oops", "Yes"} cancel button {"Oops"}
If I still needed to hide unnecessary windows, then I would do it like this:
set currentApplicationName to name of current application
tell application "System Events"
set sDisk to name of startup disk as text
set visible of every process whose visible is true and name is not currentApplicationName to false
end tell
if sDisk contains "BU" then ¬
display alert "Is " & sDisk & ¬
" the correct startup disk?" message "You are working from the 'backup' disk" as critical ¬
buttons {"Oops", "Yes"} cancel button {"Oops"}
after some experimenting I implemented your suggestions and made this:
with timeout of 3600 seconds -- set to 1 hour maximum = 3600
delay 0
--
set currentApplicationName to name of current application
--
tell application "System Events"
set sDisk to name of startup disk as text
set visible of every process whose visible is true and name is not currentApplicationName to false
--
set theAlertText to "Starting up from: ===> " & sDisk & " <=== Please VARIFY!"
set theAlertMessage to "Is this indeed the Start-up disk you want to use!
If not restart after selecting the correct disk.
Your current start-up disk is:
===> " & (sDisk)
display alert theAlertText message theAlertMessage as critical buttons {"!! Oh no I need to Reboot !!", "Yes I use " & sDisk & ""} cancel button {"Yes I use " & sDisk & ""}
if result = {button returned:"!! Oh no I need to Reboot !!"} then
tell application "System Preferences"
activate
end tell
delay 1
tell application "System Events"
tell process "System Preferences"
click menu item "Startup Disk" of menu "View" of menu bar 1
end tell
end tell
end if
end tell
--
if sDisk contains "BU" then ¬
display alert "Is " & sDisk & ¬
" the correct startup disk?" message "
===> Please VARIFY! " & sDisk & " <===
You are working from the 'backup' disk = = = >. " & sDisk & " " as critical ¬
buttons {"!! Oh no I need to Reboot !!", "Yes I use " & sDisk & ""} cancel button {"Yes I use " & sDisk & ""}
if result = {button returned:"!! Oh no I need to Reboot !!"} then
tell application "System Preferences"
activate
end tell
delay 1
tell application "System Events"
tell process "System Preferences"
click menu item "Startup Disk" of menu "View" of menu bar 1
end tell
end tell
end if
--
end timeout -- If x hours isn't enough, use a larger number.
tell application "System Events"
set sDisk to name of startup disk as text
set visible of every process whose visible is true and name is not currentApplicationName to false
--
set theAlertText to "Starting up from: ===> " & sDisk & " <=== Please VARIFY!"
set theAlertMessage to "Is this indeed the Start-up disk you want to use!
If not restart after selecting the correct disk.
Your current start-up disk is:
===> " & (sDisk)
display alert theAlertText message theAlertMessage as critical buttons {"!! Oh no I need to Reboot !!", "Yes I use " & sDisk & ""} cancel button {"Yes I use " & sDisk & ""}
if result = {button returned:"!! Oh no I need to Reboot !!"} then
tell application "System Preferences"
activate
end tell
delay 1
tell application "System Events"
tell process "System Preferences"
click menu item "Startup Disk" of menu "View" of menu bar 1
end tell
end tell
end if
end tell
I will repeat: displaying alert on the front is job of current application only (not Finder, not System Events and so on). So, you should avoid wrapping the display alerts statements with tell Finder block, with tell System Events block and so on:
------------------- hide windows, except of current application's windows ---------------------------
set currentApplicationName to name of current application
tell application "System Events"
set sDisk to name of startup disk as text
set visible of every process whose visible is true and name is not currentApplicationName to false
end tell
--------------------------------- dislpay first alert --------------------------------------------------
set theAlertText to "Starting up from: ===> \"" & sDisk & "\" <=== Please VARIFY!"
set theAlertMessage to "Is this indeed the Start-up disk you want to use!
If not restart after selecting the correct disk.
Your current start-up disk is:
===> \"" & (sDisk) & "\""
display alert theAlertText message theAlertMessage as critical ¬
buttons {"!! Oh no I need to Reboot !!", "Yes I use \"" & sDisk & "\""} cancel button {"Yes I use \"" & sDisk & "\""}
if (button returned of result) is "!! Oh no I need to Reboot !!" then my restartDialog()
-------------------------------------- dislpay second alert ------------------------------------------
if sDisk contains "BU" then
display alert "Is \"" & sDisk & "\" the correct startup disk?" message "
===> Please VARIFY! \"" & sDisk & "\" <===
You are working from the 'backup' disk = = = >. \"" & sDisk & "\"" as critical ¬
buttons {"!! Oh no I need to Reboot !!", "Yes I use \"" & sDisk & "\""} ¬
cancel button {"Yes I use \"" & sDisk & "\""}
if (button returned of result) is "!! Oh no I need to Reboot !!" then my restartDialog()
end if
-------------------------------------- restart dialog handler ------------------------------------------
on restartDialog()
tell application "System Preferences" to activate
tell application "System Events" to tell process "System Preferences"
click menu item "Startup Disk" of menu "View" of menu bar 1
end tell
end restartDialog
Also, I see no need to increase the timeout. No need with timeout block at all.
Also, no need delay after activating System Preferences, as I see (at least, on my Mac).
Also, I added quote escapes to display disk names quoted.
wow you cleaned that up a lot. and thanks KniazidisR I am learning a thing or two here.
well before I posted it I tested and needed this timeout. your script also does not work without it.
error "System Events got an error: Can’t get menu item \"Startup Disk\" of menu \"View\" of menu bar 1 of process \"System Preferences\"." number -1728 from menu item "Startup Disk" of menu "View" of menu bar 1 of process "System Preferences"
so I put the timeout in again. otherwise all is well and thank you for your help.
The fact is that I found one significant error in our “wonderful” script: for the second display alert to be able to appear, you should use a separate (different) “Cancel” button in the first display alert :
------------------- hide windows, exept of current application's windows ---------------------------
set currentApplicationName to name of current application
tell application "System Events"
set sDisk to name of startup disk as text
set visible of every process whose visible is true and name is not currentApplicationName to false
end tell
--------------------------------- dislpay first alert --------------------------------------------------
set theAlertText to "Starting up from: ===> \"" & sDisk & "\" <=== Please VARIFY!"
set theAlertMessage to "Is this indeed the Start-up disk you want to use!
If not restart after selecting the correct disk.
Your current start-up disk is:
===> \"" & (sDisk) & "\""
display alert theAlertText message theAlertMessage as critical ¬
buttons {"!! Oh no I need to Reboot !!", "Cancel", "Yes I use \"" & sDisk & "\""} cancel button {"Cancel"}
if (button returned of result) is "!! Oh no I need to Reboot !!" then my restartDialog()
-------------------------------------- dislpay second alert ------------------------------------------
if sDisk contains "BU" then
display alert "Is \"" & sDisk & "\" the correct startup disk?" message "
===> Please VARIFY! \"" & sDisk & "\" <===
You are working from the 'backup' disk = = = >. \"" & sDisk & "\"" as critical ¬
buttons {"!! Oh no I need to Reboot !!", "Yes I use \"" & sDisk & "\""} ¬
cancel button {"Yes I use \"" & sDisk & "\""}
if (button returned of result) is "!! Oh no I need to Reboot !!" then my restartDialog()
end if
-------------------------------------- restart dialog handler ------------------------------------------
on restartDialog()
tell application "System Preferences" to activate
delay 1
tell application "System Events" to tell process "System Preferences"
click menu item "Startup Disk" of menu "View" of menu bar 1
end tell
end restartDialog
Because when you define the button “Yes, I will use this disk” as cancel button (in the first alert), then the script is cancelled after pressing this button. You should define other button as cancel button, or not define cancel button at all, to continue with verifying in the second alert.
Now, to avoid the problem with delay. You can use instead of hardcoded delay (delay 1, or delay 3.5) repeat loop:
on restartDialog()
tell application "System Preferences" to activate
tell application "System Events" to tell process "System Preferences"
repeat until exists menu item "Startup Disk" of menu "View" of menu bar 1
delay 0.2
end repeat
click menu item "Startup Disk" of menu "View" of menu bar 1
end tell
end restartDialog
Sorry for the delay in reply. A friend downloaded a trojan and I spend all day fixing the computer and making sure it was free of malicious stuff.
Hurray for 3-2-1 backups!
OK I see your point, and this could be important in some cases.
Thanks for pointing it out, it helps me to learn more fine aspects of AS. So, in some scripts/situations, this could be important. However, if one is happy with the disk that is the start-up disk, it is fine to stop the scripts. As the aim is to alert people (and me) that we are not working in the main disk.
See the “BU” indicator is there because any disk I use that is not the main disk has BU in the name.
That is a good one and I will incorporate it! Thanks a million.
The script runs at startup. And if windows are open from the last time the dialogue disappears under the other windows and might even hide. Is there a way to keep it permanently on top till clicked away?
folks the script works mostly but one problem remains that I am unable to solve. hope somebody is able to help me out here as I am not able to fix it.
I have 3 disks attached to my computer all with FileVault enabled. so, when this script runs at start-up, the unlocking/password windows of the disks appear also. these hide the script window and all I am left with is a bouncing icon in the sidebar.
Is there a way to force it to the top or at least have it stay visible under the unlocking/password windows?
Here is the script as is.
delay 5
------------------- hide windows, except of current application's windows ---------------------------
set currentApplicationName to name of current application
tell application "System Events"
set sDisk to name of startup disk as text
set visible of every process whose visible is true and name is not currentApplicationName to false
end tell
--------------------------------- dislpay first alert --------------------------------------------------
set theAlertText to "Starting up from: ===> \"" & sDisk & "\" <=== Please VARIFY!"
set theAlertMessage to "Is this indeed the Start-up disk you want to use!
If not restart after selecting the correct disk.
Your current start-up disk is:
===> \"" & (sDisk) & "\"
"
display alert theAlertText message theAlertMessage as critical ¬
buttons {"!! Oh no I need to Reboot !!", "Cancel", "Yes I use \"" & sDisk & "\""} cancel button {"Cancel"}
if (button returned of result) is "!! Oh no I need to Reboot !!" then my restartDialog()
-------------------------------------- display second alert ------------------------------------------
if sDisk contains "BU" then
display alert "Is \"" & sDisk & "\" the correct startup disk?" message "
===> Please VARIFY! \"" & sDisk & "\" <===
You are working from the 'backup' disk = = = >. \"" & sDisk & "\"" as critical ¬
buttons {"!! Oh no I need to Reboot !!", "Yes I use \"" & sDisk & "\""} ¬
cancel button {"Yes I use \"" & sDisk & "\""}
if (button returned of result) is "!! Oh no I need to Reboot !!" then my restartDialog()
end if
-------------------------------------- end time-out ------------------------------------------
--end timeout
-- If x hours isn't enough, use a larger number.
-------------------------------------- restart dialogue handler ------------------------------------------
on restartDialog()
tell application "System Preferences" to activate
tell application "System Events" to tell process "System Preferences"
repeat until exists menu item "Startup Disk" of menu "View" of menu bar 1
delay 0.2
end repeat
click menu item "Startup Disk" of menu "View" of menu bar 1
end tell
end restartDialog