AppleScript - Unmount Timeout

Hi, I'm not sure if anyone can help me with how this code of mine is arranged but I keep getting a timeout error and I'm not sure why. Firstly, my script will quit an application that's been running for a predetermined time which works fine and I allow a delay of 15 minutes to make sure that quit fully happens before moving on. I've not detailed that script as it works successfully. However, I then want to unmount the external drive that the application was using. The fist part of the script basically, closes the finder windows so the Mac doesn't come up with the "can't eject as the <external drive> is in use by finder" and then unmount but for some reason I keep getting the timeout error. I've tried putting a timout time to be longer but still the same message appears. Perhaps all it needs is a longer timeout duration or is there something wrong with my script. The scripts are as follows:


on run {input, parameters}


tell application "Finder" to close every window


tell application "Finder" to eject (every disk whose ejectable is true)


with timeout of 900 seconds


--wait 15 minutes before timeout


end timeout


end run


Perhaps, I need more to this code?


The last part of the script (again, not show) just tells finder to shut down the Mac but it never get's to that point because of the timeout.


Any ideas please. I'm not sure what else to try.


Thank you.

Mac mini 2018 or later

Posted on Feb 10, 2022 5:54 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 10, 2022 6:44 AM

First of all, the timeout clause is not a delay by itself but is intended to give the actual AppleScript command(s) that it encloses up to 900 seconds. A with timeout … end timeout with no enclosed command(s) does nothing, and falls through to the next command.


Secondly, If you are using an APFS formatted Time Machine drive, your eject command will eject every single backup in that drive, before it ejects the drive itself. If you do not want that, then you need to rephrase your command:


use scripting additions

# prevent these drives from being ejected
set excludes to {"Data", "TMX8_SUR"}

tell application "Finder"
	with timeout of 900 seconds
		get every disk whose ejectable is true and name is not in excludes and name does not end with "backup"
	end timeout
end tell
return



8 replies
Question marked as Top-ranking reply

Feb 10, 2022 6:44 AM in response to Extermi-Nate

First of all, the timeout clause is not a delay by itself but is intended to give the actual AppleScript command(s) that it encloses up to 900 seconds. A with timeout … end timeout with no enclosed command(s) does nothing, and falls through to the next command.


Secondly, If you are using an APFS formatted Time Machine drive, your eject command will eject every single backup in that drive, before it ejects the drive itself. If you do not want that, then you need to rephrase your command:


use scripting additions

# prevent these drives from being ejected
set excludes to {"Data", "TMX8_SUR"}

tell application "Finder"
	with timeout of 900 seconds
		get every disk whose ejectable is true and name is not in excludes and name does not end with "backup"
	end timeout
end tell
return



Feb 10, 2022 11:07 AM in response to Extermi-Nate

As Viking mentioned, with timeout does not cause a delay, it just allows the enclosing commands to take up to that time to execute before timing out.


If you want an actual delay in your script execution, use the delay command:


doSomething()
delay 15 * minutes 
doSomethingElse()


You can specify any time metric in the delay command. the default is seconds, but you can use 'minutes', 'hours', 'days', etc. to use other time scales.

Feb 10, 2022 12:33 PM in response to Extermi-Nate

What is this code for? What is it supposed to do, at a high level? Why do you need it?


You shouldn't ever get an error message saying a drive is in use by the Finder. That suggests there is something fundamentally wrong.


Mounting and unmounting volumes are tricky operations. You can't use timeouts at all. It might work half the time, but then you have a script that breaks half the time. Ideally, you associate some action with the result of mounting and unmounting, and execute that when the event occurs. But even here, these file system operations are not particularly reliable, so any script that uses this is likely to have a high failure rate.

Feb 10, 2022 3:18 PM in response to etresoft

The code launches an app and requests the duration I’d like it to run. It reads and writes content on an external hard drive. After the end of the specified period the app closes (which can take a little while), the hard drive ejects and the Mac shuts itself down. As a bonus, it empties the trash on its way out!


As for finder, I’m not sure if it’s a fundamental error but I would get the error message whether manually ejecting or scripted if a window was open displaying the contents of the drive. So closing all open finder windows ensures this isn’t causing any issues.


I appreciate what you’re saying about the inconsistencies or success rate of using unmounting in scripts but for now, Viking’s suggestion is working. Time will tell :)



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript - Unmount Timeout

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.