|
|
#1 |
|
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 |
|
|
|
|
|
#2 |
|
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. |
|
|
|
|
|
#3 |
|
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 ?
|
|
|
|
|
|
#4 |
|
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 |
|
|
|
|
|
#5 |
|
Triple-A Player
Join Date: Oct 2004
Posts: 70
|
__________________
Want Gmail? PM me! |
|
|
|
|
|
#6 | |||||||||||||||||||||||
|
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.
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 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. |
|||||||||||||||||||||||
|
|
|
|
|
#7 |
|
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.
|
|
|
|
|
|
#8 |
|
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
|
|
|
|
|
|
#9 | |||||||||||||||||||||||
|
MVP
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
|
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
|
|||||||||||||||||||||||
|
|
|
|
|
#10 |
|
Triple-A Player
Join Date: Jul 2003
Posts: 159
|
Thanks a lot bramley! it worked flawlessly !!!
Software development on demand... |
|
|
|
|
|
#11 |
|
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? |
|
|
|
|
|
#12 | |||||||||||||||||||||||
|
MVP
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
|
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. |
|||||||||||||||||||||||
|
|
|
|
|
#13 | ||||||||||||||||||||||||||||||||||||||||||
|
Triple-A Player
Join Date: Jul 2003
Posts: 159
|
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 )
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! |
||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#14 |
|
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 |
|
|
|
|
|
#15 |
|
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 |
|
|
|
|
|
#16 |
|
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 |
|
|
|
|
|
#17 |
|
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. |
|
|
|
|
|
#18 |
|
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. |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|