The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   The Coat Room (http://hintsforums.macworld.com/forumdisplay.php?f=8)
-   -   Have Applescript, will travel... (http://hintsforums.macworld.com/showthread.php?t=76158)

Jay Carr 08-01-2007 10:56 PM

Have Applescript, will travel...
 
So, I'm learning Applescript right now. And I'm quickly finding that the best way to learn is to just get a project and start working on it, especially since I already know Java. Thus, the concepts are there, I just need to learn a new Vocab.

That said, I'm looking for projects. If anyone here has a small script they would like done (no matter how inane), I've got the time right now to give it a go (though I make no guarantee's). It's just a fun and productive way for me to learn a new skill.

I especially would appreciate anyone who can give me a project that gives me an excuse to build a stand alone application out of AppleScript. That would be fun...PM me if you're interested.

fazstp 08-02-2007 01:05 AM

How about a script that will create a compressed archive ( Disk Utility dmg ) of a folder when it's dragged onto it, named by reverse date stamp ( ie. 20070802.dmg ) and saved to a pre-defined backup folder (ie. ~/Documents/Backups/ ).

Thanks :)

Anti 08-02-2007 02:54 AM

Quote:

Originally Posted by fazstp (Post 397773)
How about a script that will create a compressed archive ( Disk Utility dmg ) of a folder when it's dragged onto it, named by reverse date stamp ( ie. 20070802.dmg ) and saved to a pre-defined backup folder (ie. ~/Documents/Backups/ ).

Thanks :)

I'd be interested in having a copy of this Applescript, if no one would mind ^_^

ArcticStones 08-02-2007 03:17 AM

.
Good idea, Fazstp!

Jay Carr 08-02-2007 03:47 AM

Do you want the destination hard wired, or do you want a field to enter it into? And I'm pretty sure that will be pretty easy to do.

appleman_design 08-02-2007 08:48 AM

i'll take a script like that too...thxs

NovaScotian 08-02-2007 09:19 AM

In the spirit of your learning curve, I won't post an answer here, but I will say that Fazstp's proposal is quite doable. I have a very neat way to do the reversed (i.e. sortable) date in plain vanilla AppleScript, but you should look for one yourself.

cwtnospam 08-02-2007 09:33 AM

Are you Applescripting Disk Utility or using hdiutil in a shell script? I've done it with hdiutil, but am having trouble if the folder or volume name contains a space. For some reason the POSIX path isn't backslashing the way I think it should.

Edit: Never mind. I forgot about 'quoted form of...'

NovaScotian 08-02-2007 11:03 AM

Quote:

Originally Posted by cwtnospam (Post 397842)
Edit: Never mind. I forgot about 'quoted form of...'

Precisely. If you have an HFS path (from set F to choose folder without invisibles, say) then:

set P to quoted form of posix path of F works fine with hdiutil in the form of

do shell script "whatever " & P

schneb 08-02-2007 01:36 PM

I am in dire need for an AppleScript that would transfer my Spotlight comments into IPTC so that my 6,000 plus photos will have the information as part of the file and not part of the .DSSTORE file.

This may be a hard one.

NovaScotian 08-02-2007 03:06 PM

Quote:

Originally Posted by schneb (Post 397889)
I am in dire need for an AppleScript that would transfer my Spotlight comments into IPTC so that my 6,000 plus photos will have the information as part of the file and not part of the .DSSTORE file.

This may be a hard one.

Depends what software you want to use to speak to the IPTC.

fazstp 08-02-2007 06:49 PM

Quote:

Originally Posted by Zalister (Post 397810)
Do you want the destination hard wired, or do you want a field to enter it into? And I'm pretty sure that will be pretty easy to do.

I guess pre-defined but with a browse option to select a new default.

Jay Carr 08-02-2007 07:25 PM

Well it seems like fazstp would be a good on to do...if I knew shell commands...which means this project is even better for me because now I get to use shell commands in my script, thus I'll learn even more! ;).

There's a program called DMG Tool that actually turns all of the hdiutil calls into scripts. But the documentation was sooooo poor that I'm going to skip it.

As for scripting Disk Utility, it has no library for some stupid reason, so I'm going to have to go with hdiutil. Like I said before, I look forward to it. But, as I said before, don't hold your breath! I'm trying to learn here, it could take a while.

Yours...

NovaScotian 08-02-2007 07:54 PM

Quote:

Originally Posted by schneb (Post 397889)
I am in dire need for an AppleScript that would transfer my Spotlight comments into IPTC so that my 6,000 plus photos will have the information as part of the file and not part of the .DSSTORE file.

This may be a hard one.

Looking into this, it's not actually hard. The difficulty will be parsing your comment into appropriate IPTC categories, unless you want only one or your comments are particularly well organized.

Reading the comment is:

tell app "Finder" to set cmt to comment of an_alias_to_a_commented_file

standard IPTC categories include:

Caption, Caption_writer, Headline, Instructions, Byline, Byline_Title, Credit, Source, title, unknown1, City, State, Country, reference_, Categories, Supplemental_Categories, unknown2, Keywords, Copyright_notice, _URL, event_, people, itemlist

GraphicConverter can set those individually, but be warned that setting overwrites anything already there.

NovaScotian 08-02-2007 07:57 PM

Quote:

Originally Posted by Zalister (Post 397987)
---
As for scripting Disk Utility, it has no library for some stupid reason, so I'm going to have to go with hdiutil. Like I said before, I look forward to it. But, as I said before, don't hold your breath! I'm trying to learn here, it could take a while.

The "stupid" reason is that it is not scriptable, and GUI scripting is a weak way to go. Go to your terminal and type man hdiutil. Ask questions.

cwtnospam 08-02-2007 08:17 PM

:o That makes my earlier question pretty dumb. I had just assumed Disk Utility was scriptable, but using hdiutil would be faster. :o

shufflebear 08-02-2007 08:20 PM

No such thing as a dumb question....Really

ThreeDee 08-02-2007 08:35 PM

After learning AS, you can make fully working programs (menus, buttons, windows) in Xcode with AppleScript Studio.

BTW, People don't usually type the abbreviation of AppleScript Studio.

NovaScotian 08-02-2007 08:42 PM

Quote:

Originally Posted by shufflebear (Post 398003)
No such thing as a dumb question....Really

There are, however, questions that make you feel dumb when you ask them. Been there, done that a few times -- have the tee shirt somewhere.

Anti 08-03-2007 02:41 AM

Quote:

Originally Posted by ThreeDee (Post 398009)
BTW, People don't usually type the abbreviation of AppleScript Studio.

That made me fall out of my chair laughing.

Jay Carr 08-03-2007 03:50 AM

Quote:

Originally Posted by ThreeDee (Post 398009)
After learning AS, you can make fully working programs (menus, buttons, windows) in Xcode with AppleScript Studio.

BTW, People don't usually type the abbreviation of AppleScript Studio.

*Giggle* (my wife still gets mad at me for doing that too...)

Um, out of curiosity, how does one go about doing that? I could see me doing that with Java or even learning object-c to do it. Why would anyone use AppleScript, wouldn't it be considerably slower?

NovaScotian 08-03-2007 12:53 PM

Yes it's often slower that C, but not slower than Java typically. People do it because they want to write an app that involves a good deal of interaction with other applications and they want easy access to Apple Events.

tlarkin 08-03-2007 01:12 PM

can you make an apple script that transfers money from your bank account to mine?;)

I would look into making apple scripts to automate certain mundane tasks, like burning a CD, or composing an email or anything that would require some sort of manual data entry.

schneb 08-03-2007 07:14 PM

Quote:

Originally Posted by NovaScotian (Post 397995)
Looking into this, it's not actually hard. The difficulty will be parsing your comment into appropriate IPTC categories, unless you want only one or your comments are particularly well organized.

Reading the comment is:

tell app "Finder" to set cmt to comment of an_alias_to_a_commented_file

standard IPTC categories include:

Caption, Caption_writer, Headline, Instructions, Byline, Byline_Title, Credit, Source, title, unknown1, City, State, Country, reference_, Categories, Supplemental_Categories, unknown2, Keywords, Copyright_notice, _URL, event_, people, itemlist

GraphicConverter can set those individually, but be warned that setting overwrites anything already there.

I only want my Spotlight words to be written in the IPTC Comments box so that I can search my photos in Windows. I have an Automator Action that does it, unfortunately, it uses Photoshop to resave the photo with no way to adjust the JPEG settings! So it saves it as a small, low quality image. Grrrr. I tried saving the photo as another file, but now my "created" dates are messed up, and all my Spotlight comments are gone.

GraphicConverter would be just fine as long as it does not mess with the photo itself. Is that possible? I have both GraphicConverter and the latest Photoshop if that helps. All I want is a batch application that will "Copy Spotlight Comments" then "Paste results in IPTC comments".

You would not believe how hard such a thing is to find!

mark hunte 08-03-2007 08:04 PM

I wrote a script a while back see this thread on bbs.applescript.net .

It uses GraphicConverter to write IPTC from user input, But is should be very easy to convert it to your needs

NovaScotian 08-03-2007 08:16 PM

Hi Schneb;

This should do it (try it on a duplicate set, of course):

Code:

set F to choose folder
tell application "Finder"
        try
                set P to files of entire contents of F as alias list -- digs down through enclosed folders
        on error -- only one file found in chosen folder; a bug for as alias list
                set P to files of entire contents of F as alias as list -- deals with only one
        end try
        set K to {"GIF document", "GIF Image", "JPEG document", "JPEG Image", "PNG document", "TIFF document", "TIFF Image"} -- should include your photos
        repeat with I in P -- iterate through the pics
                if kind of F is in K then
                        set C to comment of F -- get the comment from the Finder
                        tell application "GraphicConverter" to set file iptc of F to {C} -- set it with GC
                end if
        end repeat
end tell

To check a file use:

Code:

set F to choose file of type {"public.image"} without invisibles with prompt "Choose a single image"
tell application "GraphicConverter" to get file iptc of F


schneb 08-06-2007 01:32 PM

Thanks NovaScotian, I gave it a try and it did not work. I made sure GraphicsCoverter was running and tried again--nothing. I then changed the application to Adobe Photoshop, and still, no result. Sigh...

mark hunte 08-06-2007 01:51 PM

Quote:

Originally Posted by schneb (Post 398957)
Thanks NovaScotian, I gave it a try and it did not work. I made sure GraphicsCoverter was running and tried again--nothing. I then changed the application to Adobe Photoshop, and still, no result. Sigh...

It will depend also on your version of GC.

update to the latest version and try again.

Also, I finder when i use my script to do this, its best that GC is running before I run the script.

NovaScotian 08-06-2007 02:11 PM

Quote:

Originally Posted by schneb (Post 398957)
Thanks NovaScotian, I gave it a try and it did not work. I made sure GraphicsCoverter was running and tried again--nothing. I then changed the application to Adobe Photoshop, and still, no result. Sigh...

Did Not Work is not quite a rich enough description, Schneb. How did it fail? Did it not compile? Run but do nothing? Give you an error message?

Try this on one of your files and tell me what K is. Perhaps I've got the kinds wrong.

Code:

tell application "Finder" to set K to kind of (choose file without invisibles)

schneb 08-06-2007 02:12 PM

Sorry about that. Definitely not enough input there.

It compiled OK, I ran it, it asked for a folder, I gave it a folder, it ran, but no comments were transferred. No errors.

The result of your script is "Adobe Photoshop JPEG file"

Can you tell it to just accept whatever it is? I do have a few audio files and movie files, but it can just ignore those. I believe I only have the following in my archive... JPEG, MOV, MP3. About 90% is JPEG.

NovaScotian 08-06-2007 02:26 PM

Quote:

Originally Posted by schneb (Post 398967)
Sorry about that. Definitely not enough input there.

It compiled OK, I ran it, it asked for a folder, I gave it a folder, it ran, but no comments were transferred. No errors.

What did the little script for kind say, what version of GC are you using, and by "nothing was written" do you mean that the test script below returned nada?

Code:

set F to choose file of type {"public.image"} without invisibles with prompt "Choose a single image"
tell application "GraphicConverter" to get file iptc of F


schneb 08-06-2007 05:37 PM

Quote:

Originally Posted by NovaScotian (Post 398974)
What did the little script for kind say, what version of GC are you using, and by "nothing was written" do you mean that the test script below returned nada?

Sorry, was not sure what to do with that code. But I did give it a try and I got an error message.

"GraphicConverter got an error: An error of type -2 has occurred"

I then came to find out that any photo file that did not have any IPTC text written will give the error above. I put in some IPTC text via Photoshop CS and got the following output...
{"Emma with Robert, Aunt Mary and others.", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "-1", "", "", "", "", ""}

NovaScotian 08-06-2007 06:51 PM

I've added Adobe Photoshop JPEG file to the list (only Adobe would stick it that way; jpeg is jpeg)

Code:

set F to choose folder
tell application "Finder"
        try
                set P to files of entire contents of F as alias list -- digs down through enclosed folders
        on error -- only one file found in chosen folder; a bug for as alias list
                set P to files of entire contents of F as alias as list -- deals with only one
        end try
        set K to {"GIF document", "GIF Image", "JPEG document", "JPEG Image", "PNG document", "TIFF document", "TIFF Image", "Adobe Photoshop JPEG file"} -- should include your photos
        repeat with I in P -- iterate through the pics
                if kind of F is in K then
                        set C to comment of F -- get the comment from the Finder
                        tell application "GraphicConverter" to set file iptc of F to {C} -- set it with GC
                end if
        end repeat
end tell


schneb 08-06-2007 07:03 PM

I'm still not getting anything to write to the IPTC.

Not sure if GraphicConverter recognizes just iptc as anything. Should there be any kind of other parameter to tell it just where in the iptc--such as "description"?

Note: The following only returns the Spotlight Comments of the folder, and not the content of the files therein...

Code:

set F to choose folder
tell application "Finder"
        try
                set P to files of entire contents of F as alias list -- digs down through enclosed folders
        on error -- only one file found in chosen folder; a bug for as alias list
                set P to files of entire contents of F as alias as list -- deals with only one
        end try
        repeat with I in P -- iterate through the pics
                set C to comment of F -- get the comment from the Finder
                return C
        end repeat
end tell

Could it be that the code for determining the file type was only seeing the folder, and thereby stopped there?

Note, I changed "set C to comment of F" to "set C to comment of I" and got a better result.

The following script gave an error...

Code:

set F to choose folder
tell application "Finder"
        try
                set P to files of entire contents of F as alias list -- digs down through enclosed folders
        on error -- only one file found in chosen folder; a bug for as alias list
                set P to files of entire contents of F as alias as list -- deals with only one
        end try
        repeat with I in P -- iterate through the pics
                set C to comment of I -- get the comment from the Finder
                tell application "GraphicConverter" to set file iptc of F to {C} -- set it with GC
        end repeat
end tell

The error was "GraphicConverter got an error: An error of type -3 has occurred.

NovaScotian 08-06-2007 07:27 PM

Ahh, Schneb; apologies -- a bug. Change the F in the line below to an I

set C to comment of F -- get the comment from the Finder should be
set C to comment of I -- get the comment from the Finder

schneb 08-06-2007 07:35 PM

Yes, as well as the next line, correct?

set C to comment of I -- get the comment from the Finder
tell application "GraphicConverter" to set file iptc of I to {C} -- set it with GC

It's running without errors, however, the comments are not being written to the IPTC. Does it need a parameter to say "where" in IPTC?

I definitely feel bad about hijacking Zalister's post here. I will write up a new question so we can transfer this exchange there.

Sorry Zalister!!

NovaScotian 08-06-2007 07:46 PM

Quote:

Originally Posted by schneb (Post 399054)
Yes, as well as the next line, correct?

set C to comment of I -- get the comment from the Finder
tell application "GraphicConverter" to set file iptc of I to {C} -- set it with GC

It's running without errors, however, the comments are not being written to the IPTC. Does it need a parameter to say "where" in IPTC?

I definitely feel bad about hijacking Zalister's post here. I will write up a new question so we can transfer this exchange there.

Sorry Zalister!!

I've rewritten the names of the variables here and this should work (not because of the name change, but because I've checked that they are consistent). Sorry for all the grief, Schneb; I didn't check this against a batch of files with comments because I don't have any and would have had to do a bunch.

If you don't say "where" as in Mark's script, the comment is just the first item in the list. I can't check that because I don't have photoshop.

Code:

set Fldr to choose folder
tell application "Finder"
        try
                set Pics to files of entire contents of Fldr as alias list -- digs down through enclosed folders
        on error -- only one file found in chosen folder; a bug for as alias list
                set Pics to files of entire contents of Fldr as alias as list -- deals with only one
        end try
        set tKinds to {"GIF document", "GIF Image", "JPEG document", "JPEG Image", "PNG document", "TIFF document", "TIFF Image", "Adobe Photoshop JPEG file"} -- should include your photos
        repeat with onePic in Pics -- iterate through the pics
                if kind of onePic is in tKinds then
                        set Cmt to comment of onePic -- get the comment from the Finder
                        tell application "GraphicConverter" to set file iptc of onePic to {Cmt} -- set it with GC
                end if
        end repeat
end tell


mark hunte 08-07-2007 09:37 AM

I have tested this and it works.
Code:

set F to choose folder
tell application "Finder"
        --try
        set P to (files of entire contents of F) -- digs down through enclosed folders
        --on error -- only one file found in chosen folder; a bug for as alias list
        --set P to files of entire contents of F -- deals with only one
        --end try
        set K to {"GIF document", "GIF Image", "JPEG document", "JPEG Image", "PNG document", "TIFF document", "TIFF Image", "Adobe Photoshop JPEG file"} -- should include your photos
        repeat with I from 1 to number of items in P -- iterate through the pics
                set this_item to item I of P as alias
                if kind of this_item is in K then
                        set C to («class ktxt» of ((comment of this_item as string) as record)) -- get the comment from the Finder
                       
                        log C
                        tell application "GraphicConverter" to set file iptc of this_item to {C} -- set it with GC
                end if
        end repeat
end tell


schneb 08-07-2007 12:19 PM

NovaScotian, you are brilliant. It indeed works! I tested it on a folder full of photos and it wrote all my Spotlight comments to the description IPTC, just like I wanted. Now if I ever lose my .DS_Store files, I will not pine because of the loss of Spotlight comments--it is now a part of the photo itself!

Thanks so much for your time on this.

Schneb

NovaScotian 08-07-2007 12:46 PM

More than welcome, Schneb; Sorry it took so long to debug it. :rolleyes:

cwtnospam 08-07-2007 12:54 PM

Zalister, have you written a script for fazstp's compressed archive? I'm curious to see if my solution is different from others. :)

mark hunte 08-07-2007 01:28 PM

Sorry no disrespect to NovaScotian who has taught me a lot ;)

But his script in post #37 did not work, that is why I posted a corrected one

The reason the script did not work was:
Code:

set Fldr to choose folder
should have been
Code:

set F to choose folder
The as alias list was not needed, and was not broken down in to items later on, which is what the script wants,( it maybe a bug, in how this is handled???).
But with or without the alias list in place, if you use the line:
Code:

tell application "GraphicConverter" to set file iptc of (onePic as alias) to {Cmt} -- set it with GC
(note the as alias)
it works. In my script I did it slightly different but it has the same result

The other part that will give bad results or not write the IPTC (even though it looks like it has in the event log) is the class of the text.
Code:

set Cmt to («class ktxt» of ((comment of onePic as string) as record)) -- get the comment from the Finder
For it to work the class has to be «class ktxt» and then a record. I will not even pretend to understand why and it was Jacques who showed me this class trick over on bbs.applescript.net But it's the only way I have found to get the expected results.

NovaScotian 08-07-2007 02:15 PM

So several things are clear:

1) Schneb thanked the wrong person: Mark's version was the winner and clearly the one that Schneb must have used. Credit where it is due. :)

2) My script should have read (now thoroughly tested): :rolleyes:

Code:

set Fldr to choose folder
tell application "Finder"
        set Pics to files of entire contents of Fldr -- digs down through enclosed folders
        set tKinds to {"GIF document", "GIF Image", "JPEG document", "JPEG Image", "PNG document", "TIFF document", "TIFF Image", "Adobe Photoshop JPEG file"} -- should include your photos
        repeat with onePic in Pics -- iterate through the pics
                if kind of onePic is in tKinds then
                        set Cmt to comment of onePic -- get the comment from the Finder
                        tell application "GraphicConverter" to set file iptc of (onePic as alias) to {comment:Cmt} -- set it with GC
                end if
        end repeat
end tell

which worked perfectly for me without Mark's "kext" approach.

3) To check the results:

Code:

set Fldr to choose folder
set com to {}
tell application "Finder"
        set Pics to files of entire contents of Fldr -- digs down through enclosed folders
        set tKinds to {"GIF document", "GIF Image", "JPEG document", "JPEG Image", "PNG document", "TIFF document", "TIFF Image", "Adobe Photoshop JPEG file"} -- should include your photos
        repeat with onePic in Pics -- iterate through the pics
                if kind of onePic is in tKinds then
                        tell application "GraphicConverter" to set end of com to item 1 of (get file iptc of (onePic as alias))
                end if
        end repeat
end tell
com


Jay Carr 08-09-2007 03:46 PM

Pardon to all. I neglected to mention that I was heading out of town for a week and would not even get a chance to start to script until I get back. People keep telling me I need to be more forthright about my scheduling... One of these days I'll learn. Sorry again.

That said, I've been looking into learning some of the UNIX commands to get this done, and I'm working with droplets in AppleScript Studio to try and get it done. It will probably take a few days, but the wife is still out of town and I have nothing to do on Friday night so...maybe I'll be done quickly. Who knows.

Again, apologies, but I'm back and I'm going to start working on it.

fazstp 08-13-2007 08:33 AM

I uploaded a dmg backup script.

DMG Backup

Thanks to cwtnospam for the hdiutil scripting.

Sorry Zalister, I had to do some Applescripting for another project and once I got the hdiutil method from cwtnospam it was a short leap to the backup script I originally asked you for.

schneb 08-13-2007 01:38 PM

Quote:

Originally Posted by NovaScotian (Post 399269)
Schneb thanked the wrong person: Mark's version was the winner and clearly the one that Schneb must have used. Credit where it is due.

To avoid thread hijacking, I started another. There I gave credit to those that deserved it. Thanks to both of you, I have now successfully annotated all of my 14,000 photos with IPTC comments. It even searches in iPhoto '08! Another advantage to that great script, is that it choked if there was no Spotlight Comment, which forced me to make sure all photos had a comment. It also choked if I had inadvertently saved a JPEG file as a PSD file (hence acting as if it were a corrupt file). I had about 20 of these, and the script compelled me to fix these renegades.

Again, a big THANK YOU to both Mark Hunte and NovaScotian!

Jay Carr 08-13-2007 06:56 PM

fazstp -- Don't worry about it. I'm beginning to think I should just figure out a useful script myself and execute on it. These forums don't work at the same glacial pace that I do :).

fazstp 08-13-2007 10:13 PM

Quote:

Originally Posted by Zalister (Post 400816)
These forums don't work at the same glacial pace that I do :).

Have you found yourself working any faster as a result of global warming?

(Sorry :o glacier joke)

Jay Carr 08-14-2007 02:08 AM

No, but every now and then I feel my will to work on scripts evaporating into thin air.

(Two can play at that game ;).)


All times are GMT -5. The time now is 01:06 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, 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.