Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ciphershort

macrumors newbie
Original poster
Feb 24, 2014
6
6
Hello All,

I've written a bash script that renames and copies files from one directory to another using rsync. I'd like to take it a step further and write an AppleScript Folder Action that sees my drive in the /Volumes directory and auto opens Terminal and runs my script (I have some output I want to see which is why I want Terminal to open).

My problem is getting the stupid AppleScript to see my drive. Here's what I've gotten from the Internet and I can't seem to make it work:

Code:
on adding folder items to this_folder after receiving these_items
	repeat with this_item in these_items
		if (current_item as text) contains "USBDRIVE" then
			display notification "USBDRIVE is mounted"
		end if
	end repeat
end adding folder items to

I started using Automator and creating the Folder Action and thought that may have been the issue so I then just wrote the AppleScript and applied it to the folder and still nothing. I then just wrote a simple "display notification" line in the "Run AppleScript" module in automator and it worked so I don't think Automator is the issue.

Any help with this would be much appreciated.

Thanks!
 
Hello All,

I've written a bash script that renames and copies files from one directory to another using rsync. I'd like to take it a step further and write an AppleScript Folder Action that sees my drive in the /Volumes directory and auto opens Terminal and runs my script (I have some output I want to see which is why I want Terminal to open).

My problem is getting the stupid AppleScript to see my drive. Here's what I've gotten from the Internet and I can't seem to make it work:

Code:
on adding folder items to this_folder after receiving these_items
	repeat with [B]this_item[/B] in these_items
		if ([B]current_item[/B] as text) contains "USBDRIVE" then
			display notification "USBDRIVE is mounted"
		end if
	end repeat
end adding folder items to

I started using Automator and creating the Folder Action and thought that may have been the issue so I then just wrote the AppleScript and applied it to the folder and still nothing. I then just wrote a simple "display notification" line in the "Run AppleScript" module in automator and it worked so I don't think Automator is the issue.

Any help with this would be much appreciated.

Thanks!

You can always redirect output to a file. Start by providing your loop with some input and when everything works as expected you can then put it into a folder action handler. You can also check if a disk exists with something like this :

Code:
tell application "Finder"
	if exists disk "USBDRIVE" then
		set isMounted to true
	else
		set isMounted to false
	end if
end tell

if isMounted then
	display notification "USBDRIVE is mounted"
end

A Folder Action processes file/folders dropped in the watched folder. I'm under the impression that you seem to have a different notion of what it really does.

Tip : Notice the bold items in the folder action handler. These_items is a list of aliases e.g. HFS path (colon separated), which takes the form "disk:item:subitem:subsubitem:...:item. Terminal uses POSIX paths.
 
Thanks for the reply! I know what folder actions do, it's just when I searched for automatically running a script when a drive is mounted, the script in my original post is what popped up everywhere and it didn't work. That being said, your solution worked PERFECTLY! I'm not sure why I didn't find a solution using Finder to see if the disk exists. That seems way easier. This is what I ended up doing.

Code:
tell application "Finder"
	if exists disk "USBDRIVE" then
		set isMounted to true
	else
		set isMounted to false
	end if
end tell

if isMounted then
	display notification "USBDRIVE is mounted"
	tell application "Terminal"
		activate
		do script "syncmystuff"
	end tell
end if

My script then goes and renames my files through a series of sed commands and then uses rsync to transfer everything to my hard drive. Thanks again for your help!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.