Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 09-29-2004, 11:37 AM   #1
Pedro Estarque
Triple-A Player
 
Join Date: Jul 2003
Posts: 159
Open on mount Script

I would like to make an AppleScript more or less like this:

on mount disk "flash 512"
open last file created on disk "flash 512"
wait 15 sec
unmount disk "flash 512"

can this be done with AppleScript or do I need a more powerful language ?
Thanks
Pedro Estarque is offline   Reply With Quote
Old 09-30-2004, 07:16 AM   #2
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Applescript can do this as a folder action - but you cannot mount a volume, open a file, and then eject the volume, with the file still open. A system error will occur.

You will have to either close the file before ejecting, copy/move the file to another location before opening it, or not eject the volume until some other event (about which you should provide info) takes place.
bramley is offline   Reply With Quote
Old 09-30-2004, 10:08 AM   #3
Pedro Estarque
Triple-A Player
 
Join Date: Jul 2003
Posts: 159
you can open a file in photoshop and eject the disk while it's still opened. I do this all the time with no system error what so ever. I think preview also does it without a glitch. How would you write this applescript ?
Pedro Estarque is offline   Reply With Quote
Old 10-18-2004, 12:16 PM   #4
Vocal Velocity
Prospect
 
Join Date: Jan 2002
Location: Amarillo
Posts: 16
mounted Disks script

Use Finder to determine is a disk is mounted...Once mounted then do something.

Code:
tell application "Finder"
		set mounted_Disks to list disks
		display dialog result as string
		--set mounted_Disks to every disk whose (ejectable is true) -- this line works great with CD's or DVD's
		if mounted_Disks does not contain "NameofDisk" then
			mount volume "cifs://DNSorIPaddress/SharedVolume/" as user name "User" with password "pwd"
		end if
	end tell
Vocal Velocity is offline   Reply With Quote
Old 10-18-2004, 03:31 PM   #5
Jacob Spindel
Triple-A Player
 
Join Date: Oct 2004
Posts: 70
Try this:

http://www.macupdate.com/info.php/id/15188
__________________
Want Gmail? PM me!
Jacob Spindel is offline   Reply With Quote
Old 10-18-2004, 04:03 PM   #6
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Sorry, Pedro, I've been very busy these last few days, and hadn't noticed your reply.

Quote:
Originally Posted by Pedro Estarque
you can open a file in photoshop and eject the disk while it's still opened. I do this all the time with no system error what so ever. I think preview also does it without a glitch. How would you write this applescript ?

Well, this is not quite the same thing. These applications open the file so it can be read, and then close it once it has been read. Assuming these are the sort of apps you'll be using then there shouldn't be any problem with the following Applescript.

Code:
property start_date : date "Friday, September 17, 1965 12:00:00 am"

on adding folder items to this_folder after receiving these_items
	repeat with aItem in these_items
		tell application "Finder"
			if (local volume of aItem) and (name of aItem is "flash 512") then
				set file_list to every file of entire contents of aItem
				set last_date to start_date
				repeat with aFile in file_list
					if ((modification date of aFile) > last_date) then
						set last_date to modification date of aFile
						set last_file to aFile
					end if
				end repeat
				open last_file
				delay 15
				eject aItem
			end if
		end tell
	end repeat
end adding folder items to
You can get some info on setting up folder actions on your machine from here: http://www.apple.com/applescript/fol...ons/index.html

You need the above script to be in either /Library/Scripts or ~/Library/Scripts before it can be attached.

You need to attach the action to the /Volumes folder, which is done from Folder Action Setup. Press shift-command-G to get the UNIX dialog box and enter '/Volumes' Make sure the card is not mounted before installing the script. Once everything is OK, stick the card in and it should work. If you run into any difficulties then post back (before Friday - I'm away again)

PS Are you working with image files only?

Last edited by bramley; 10-18-2004 at 04:05 PM.
bramley is offline   Reply With Quote
Old 10-18-2004, 07:28 PM   #7
Pedro Estarque
Triple-A Player
 
Join Date: Jul 2003
Posts: 159
Thanks a lot everyone I'll try this at work tomorrow and I'll post the results here.
Pedro Estarque is offline   Reply With Quote
Old 10-19-2004, 09:58 AM   #8
Pedro Estarque
Triple-A Player
 
Join Date: Jul 2003
Posts: 159
Thanks a lot bramley!!!
It worked great! I tried to make a slight modification but my poor AppleScripting skills wouldn't help. I found it would be more functional if, instead of ejecting after 15 sec, the script presented a dialog box asking if I wanted to eject or not. So I tried this:

Code:
property start_date : date "Friday, September 17, 1965 12:00:00 am"

on adding folder items to this_folder after receiving these_items
        repeat with aItem in these_items
                tell application "Finder"
                        if (local volume of aItem) and (name of aItem is "flash 512") then
                                set file_list to every file of entire contents of aItem
                                set last_date to start_date
                                repeat with aFile in file_list
                                        if ((modification date of aFile) > last_date) then
                                                set last_date to modification date of aFile
                                                set last_file to aFile
                                        end if
                                end repeat
                                open last_file
                                display dialog "Eject disk ?" buttons {"Cancel", "OK"} default button 2
                                If the button_pressed is "OK" then
                                eject aItem
                        end if
                     end if
                end tell
        end repeat
end adding folder items to
What am I doing wrong?
Pedro Estarque is offline   Reply With Quote
Old 10-19-2004, 11:29 AM   #9
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Quote:
Originally Posted by Pedro Estarque
I found it would be more functional if, instead of ejecting after 15 sec, the script presented a dialog box asking if I wanted to eject or not. What am I doing wrong?

The corrected code is:
Code:
set response to display dialog "Eject disk ?" buttons {"Cancel", "OK"} default button 2
if button returned of response is "OK" then
            eject aItem
end if
display dialog is a function. The best way to retrieve its value is to 'return' it with the variable response. The variable is of type 'dialog reply'. Dialog replies have 'button returned' properties.
bramley is offline   Reply With Quote
Old 10-19-2004, 03:31 PM   #10
Pedro Estarque
Triple-A Player
 
Join Date: Jul 2003
Posts: 159
Thanks a lot bramley! it worked flawlessly !!!
Software development on demand...
Pedro Estarque is offline   Reply With Quote
Old 11-04-2004, 11:43 AM   #11
Strider72
Prospect
 
Join Date: Nov 2004
Posts: 2
Okay, I got Bramley's version going (#6), and it works great! (I modified the contents of the IF clause for my purposes, but the framework is the same. My version copies a file from the mounted disk, then ejects the disk.)

I'm having a strange problem now. After I run the script, Folder Actions gets deactivated, so the script (or any other folder action) doesn't work until I manually reactivate Folder Actions.

Any ideas what might cause that?
Strider72 is offline   Reply With Quote
Old 11-04-2004, 12:11 PM   #12
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Quote:
Originally Posted by Strider72
Okay, I got Bramley's version going (#6), and it works great! (I modified the contents of the IF clause for my purposes, but the framework is the same. My version copies a file from the mounted disk, then ejects the disk.)

I'm having a strange problem now. After I run the script, Folder Actions gets deactivated, so the script (or any other folder action) doesn't work until I manually reactivate Folder Actions.

Any ideas what might cause that?

To work Folder Actions needs a background application called "System Events" to be running. Your problems are probably because your script is crashing "System Events." This is almost certain to be the cause if your script makes use of any GUI scripting (also needs 'System Events')

I would checking Activity Monitor before and after using the Folder Action and see if System Events keels over before anything else.
bramley is offline   Reply With Quote
Old 11-04-2004, 01:34 PM   #13
Pedro Estarque
Triple-A Player
 
Join Date: Jul 2003
Posts: 159
Quote:
Originally Posted by Strider72
My version copies a file from the mounted disk, then ejects the disk.)

Yeh, that would be useful for me too. Maybe as run-only, not as a folder action. How did you do it? Can you post it ? (sorry no programing experience at all ... yet. I'm gonna learn it some day )

Quote:
I'm having a strange problem now. After I run the script, Folder Actions gets deactivated, so the script (or any other folder action) doesn't work until I manually reactivate Folder Actions.

Any ideas what might cause that?

It happens here too. But only rarely, it seems more like an bug from apple then from bramley's code.
I'm using it every day and it saves me a lot of time. Thanks again bramley!
Pedro Estarque is offline   Reply With Quote
Old 11-04-2004, 01:40 PM   #14
Strider72
Prospect
 
Join Date: Nov 2004
Posts: 2
"Your problems are probably because your script is crashing 'System Events.'"

You nailed it. I added a 3 second Delay and it works now. Thanks!

Pedro: I'll post in a bit. The computer in question is offline, so I can't just copy/paste. Off the top of my head, I use the "Duplicate" command
Strider72 is offline   Reply With Quote
Old 11-22-2004, 03:11 PM   #15
Pedro Estarque
Triple-A Player
 
Join Date: Jul 2003
Posts: 159
Another change

I wanted to automate my work even further ( it's getting addictive )
And I changed bramley's code a little bit more, but it failed to run although it compiles just fine. Basically I wanted to copy the last file created to the last folder created in the desktop. Here is the code:
Code:
property start_date : date "Friday, September 17, 1965 12:00:00 AM"
tell application "Finder"
	set file_list to every file of entire contents of disk "Untitled"
	set last_date to start_date
	repeat with aFile in file_list
		if ((modification date of aFile) > last_date) then
			set last_date to modification date of aFile
			set last_file to aFile
		end if
	end repeat
	set folder_list to every folder of entire contents of folder "˜/Desktop"
	set last_date to start_date
	repeat with aFolder in folder_list
		if ((modification date of aFolder) > last_date) then
			set last_date to modification date of aFolder
			set last_folder to aFolder
		end if
	end repeat
	
	copy last_file to last_folder
	
	
end tell
end
There's something wrong with the "every folder" part as the finder fails to get every folder from folder desktop. Any clues ? Thanks
Pedro Estarque is offline   Reply With Quote
Old 11-24-2004, 04:09 AM   #16
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
The problem is not the use of 'every folder' but the use of a UNIX path for the desktop. You must use a Mac path or an alias. Best way is with an alias. Below is your script with my additions/corrections in red.

Code:
property start_date : date "Friday, September 17, 1965 12:00:00 AM"

set thefolder to path to desktop
tell application "Finder"
	set file_list to every file of entire contents of disk "Untitled"
	set last_date to start_date
	repeat with aFile in file_list
		if ((modification date of aFile) > last_date) then
			set last_date to modification date of aFile
			set last_file to aFile
		end if
	end repeat
	set folder_list to every folder of entire contents of thefolder
	set last_date to start_date
	repeat with aFolder in folder_list
		if ((modification date of aFolder) > last_date) then
			set last_date to modification date of aFolder
			set last_folder to aFolder
		end if
	end repeat
	copy last_file to last_folder
end tell
You should also note that the above script will place the file in the folder which was last modified on the desktop - not the last folder created. Use 'creation date' rather than 'modification date' if creation is what is required. Check Finder's dictionary for more.
bramley is offline   Reply With Quote
Old 11-24-2004, 07:46 AM   #17
Pedro Estarque
Triple-A Player
 
Join Date: Jul 2003
Posts: 159
Thanks again !

thanks bramley, I hope I learn AppleScript enough not to bother you again.
These piece of code of yours is certainly very useful, I'm already using it in many small apps.
If anyone wants to copy the code above , please replace "copy last_file to last_folder" by duplicate as the first one does not work.
Pedro Estarque is offline   Reply With Quote
Old 11-24-2004, 09:25 AM   #18
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Do use the dictionaries, and the Applescript manual: http://docs.info.apple.com/article.html?artnum=50096 Check out resources on Apple's site too: http://www.apple.com/applescript/

It's no bother answering your questions but you will get on a lot quicker (no need to wait for people to reply to posts) if you make full use the available resources.
bramley is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 08:44 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.