Open a file automatically when a disk image mounts

I have a script in ‘folder action setup’ that triggers to open a file on a disk image as soon as it mounts. The script is in a Folder Action in Automator. In Automator the Folder Action receives files and folders to is set to Volumes.

Or is this where I go wrong, and it should be set to the name of the disk image (see below for the problem)?

This is the script:

on run {input, parameters}
	
	tell application "Finder"
		if (disk "disk1" exists) then
			tell application "Finder"
				delay 1
				activate
				select file "XX.App" of disk " disk1"
				open selection
			end tell
		end if
	end tell	
	return input
end run

and it works fine except that every time another disk mounts this script triggers again. Any tip on how to stop this? I suspect I need to point it differently but can not figure it out.

Based on the question as asked… does this mounted volume have a unique name or is it the same as other disks that are sometimes mounted? ‘disk1’ is typically the identifier of the built-in main volume. NB I’m running Sierra so the filesystem of your main disk is probably different but I don’t know what developments there have been with disk images.

In general, the folder action should launch the script every time a new entry appears in the /Volumes folder, so what you’re asking for is contradictory.

In terms of what you’re trying to do, there might be some other approaches you could take.

First of all, Automator can probably do most of what you ask with its native actions

If you want to launch an application, use ‘launch ’ rather than selecting it in the Finder and asking that to open the app. Is it an app that’s already on your system but you want to run a separate version? Is it a script app?

Finally, you could always launch the volume via a script/workflow/app rather than using the folder action method. Have the script mount the diskimage and then run the app in question. Store the script in the same folder as the disk image or stick it in the scripts menu.

Browser: Firefox 110.0
Operating System: macOS 10.12

That could be an option, I look in to that if the folder action does not work.

and BTW I am not trying to open an application, I am opening a file (DB) that is on the disk image.

Thanks for your answer KniazidisR

Aha, as I suspected. So should I point it directly at my disk image? In my sample “disk1”. Would this solve the probelm too?

And I will test your script later when I am at the machine. sounds good to me.

I realise I might have created unclarity in my posted script by calling the file XX.app and not XX.appname.

I am wanting to open a DB on the disk image. So, I want to check if the disk image is mounted and if it is not having the folder action run.

And if the disk image is already mounted than I like the folder action not to run even if other disk images or disks are mounted. Which is what is happening at present.

Sorry for the confusion.

@ KniazidisR
Hence the script you posted has the same problem it runs every time a disk is mounted.

If this was my project I would take a different approach. I would create an AppleScript “stay open application” that continuously checks (every second) if your desired disk image is mounted. Then, once it detects that your desired disk image is mounted, it will run whatever command you want it to run and then the AppleScript “stay open application” will quit itself.

The first thing I would do is mount the disk image you want to target then run this following AppleScript code, so you can find the display name of your disk image… So you can use that value in your stay open application.

tell application "System Events"
	set diskNames to displayed name of disks
end tell

Then after looking through those results… Copy the name of your desired disk image so you can paste that into the code of your AppleScript “stay open application” (property targetDisk : “name of disk image” – Name of disk image to look for)

Save this following AppleScript code as a “Stay Open Application” (after editing it to your needs)

property targetDisk : "YouTube to MP3" -- Name of disk image to look for

-- on run
tell application "System Events" to set diskNames to displayed name of disks

if diskNames contains targetDisk then
	say targetDisk & " disk image is mounted" -- Run command when target dmg mounts
	quit me -- quits this stay open app
end if
-- end run

on idle
	tell application "System Events" to set diskNames to displayed name of disks
	
	if diskNames contains targetDisk then
		say targetDisk & " disk image is mounted" -- Run command when target dmg mounts
		quit me
	else
		return 1 -- checks every second for the target dmg
	end if
end idle

If you decide that you like this approach, I can show you how to hide the stay open app, from the dock, while it is running.

Thanks wch1zpink. I would like to solve it defiantly and in the way I was doing. Still, first off, I did not know one could do this. So hanks for that. Should I change my mind I will come back to you.

The reason I would like not to do it the other way is that the Disk is not used every day, sometimes not for days. And so if I have this run in the background all the time using processor space and battery.

Please have a look at this article

https://macscripter.net/viewtopic.php?id=42503

It describes a launchd agent, which watches the /Volumes folder and runs a script when a volume appears.

The script in the thread copies files but it could do anything.

Wow, I did not know you could do this StefanK. Thanks.

However I suspect this conversation (in the link) is a bit above my skill level. And I would not know how to adapt it for my purposes. I am still hoping somebody helps me with my original approach.

Regardless of the way to be notified about changes in /Volumes you need to maintain the previous state and compare it to the new state.

For example this script writes the list of mounted volumes to the preferences folder and reads it on each call. The file will only be selected if myDisk has just been mounted.

Change the disk name in the line set myDisk to " disk1" to your proper name.

on run {input, parameters}
	set prefFilePath to POSIX path of (path to library folder from user domain) & "Preferences/mountedDisks.txt"
	try
		set myDisk to " disk1"
		set allDisks to list disks
		set previousDisks to (read prefFilePath as list)
		set shouldOpenSelection to previousDisks does not contain myDisk and allDisks contains myDisk
	on error
		set shouldOpenSelection to true
	end try
	set fd to open for access prefFilePath with write permission
	write allDisks to fd
	close access fd
	if shouldOpenSelection then
		launch application "/Volumes/" & myDisk & "/XX.app"
	end if
	return input
end run

@ StefanK

Thanks. I am on the road and will test as soon as I am back at the machine. Thanks for posting this.

@ StefanK
Hi, I just logged in remotely (was exited to know if it works) to test and unfortunately the script does not open the desired file.

I changed the disk name and the file name to the appropriate local names. However, this did not make it work. And my skill level does not allow me to see how I could change this to make it work. Sorry.

FYI I changed these lines to the roper names:
set myDisk to " disk1"
select file “XX.App” of disk myDisk

Rather than selecting the file in Finder


tell application "Finder"
	if shouldOpenSelection then
		delay 1
		activate
		select file "XX.App" of disk myDisk
		open selection
	end if
end tell
 

launch the application directly

if shouldOpenSelection then
    launch application "/Volumes/" & myDisk & "/XX.app"
end if

I edited my previous post.

@Fredrik71
Thanks. The script works and has the same problem as my original script as soon as another disk loads the script gets activated.

@StefanK
Thanks. Sorry to say the script still does not run as wished for when the disk image is loaded. I can see it runs, so that is good (little gearwheel appears in the Menu Bar when the disk image has loaded) but the file on the disk image does not open.
I also tried to adjust the script with your latest addition/comment (from yesterday 05.15) but that makes no difference.

I used a crude way to see if it could be fixed with simple code (remember low skills over here).

if shouldOpenSelection then
			tell application "Finder"
				activate
				select file "NameOfTheFileIwantto.open" of disk " disk1"
				delay 1
				open selection
				delay 1
				tell application "Finder" to open POSIX file "//volumes/disk1/ NameOfTheFileIwantto.open"
			end tell
		end if

and although this script opens the file I want to open if run on its own when the disk image is already loaded in the finder. It does not do so in the script you suggested if I have it in the folder action. I even put in some delays in the hope that could be it (was often a good trick in the good old OS 6 – 7 - 8 days). But no such luck.

Greetings and thanks. I had seen this previously but I see nowhere in my control panel where I could give the application that runs the file/DB can give this kind of permission.

Can you post a pic of it for your MP4 App?

And secondly it is a DB that I want to open that is on the disk image. It opens fine under all other circumstances. Either by double clicking it or with the script I posted in my last post. and does so on several other Mac machines.

I will use your other info to see if I find anything that stops this working here.

Question if I may, did you test the script you posted on your machine? And it works?

Thanks.

Good morning Fredrik71.

Thank you for your extensive post.

I got it to work. As a matter of fact, it worked before. Let me explain. I tested it before, and it would work. I than dismounted the disk again and would run the script a second time. At that pint it did not work.

What I realise, thanks to your instructions about the permissions (and thank you for that it made me realise that this could be a problem even though the Mac does not flag it like it does most of the time), is that the mountedDisks.txt file probably did not update between me dismounting the disk and maybe 2 minutes later mounting it again. And yes, I had seen your comment before and found all in order.

Do you, or anybody, know how often mountedDisks.txt gets updated by the system? Or how long it takes to reset after a dismount? I could find nothing on the internet about it.

Is there a script to force it to update? I could add this to the script I have to unload the disk image that I always use. I ask as there are times where it might be loaded and unloaded a few times a day.

Also, your explanation about paths (POSIX file vs file) was something I had seen but never realised so clearly. Thanks again.

I did not understand this instruction, which I think could be helpful in the future:

Can you explain a bit more what command you run in Terminal?

As to my question of the testing of the script, I am sorry if this upset you. I wanted to just make sure it was running somewhere so I could isolate the problem on my site. Sometimes folk publish a script but it was not tested.

Again, thank you (and others) for your help on this issue. I learned a thing or two.

Tranks Fredrik71 clear