Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > - AppleScript assistance

- AppleScript assistance
Thread Tools
Dedicated MacNNer
Join Date: Mar 2001
Location: not far from my GSM phone
Status: Offline
Reply With Quote
Feb 10, 2004, 05:20 AM
 
Hi. I'm relatively new to AS, and I need to do a rather simple script to automate the following task (in the background if possible):
-watch a specified folder
-when a new .fla file is found, check if it has a matching (meaning compiled) .swf file already
-if yes, do nothing with that .fla file
-if no, open that file in FlashMX04 and simply export
- optional: send a mail to a specific address
- optional: move the fla and swf to a subfolder

I don't know how to handle that, up to now I was only able to record on screen actions....

Maybe someone already has such a script, or the basic code to adapt to my problem.

1000 thanks

Greg
Lao_Tseu
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Feb 10, 2004, 12:51 PM
 
Here's what I hacked-up:

NB - you will need to change the Keystroke command (I have Command-Option-P set as Publish in my copy of Flash)
Code:
-- flashCheck((choose file) as list) -- make this script a droplet (for testing purposes) on open droppedFiles flashCheck(droppedFiles) end open -- make this script a a Folder Action for added files on adding folder items to watchFolder after receiving newItems flashCheck(newItems) end adding folder items to -- Routine that does the work to flashCheck(newItems) -- display dialog "folder script starts" buttons {"OK"} default button 1 tell application "Finder" repeat with thisFile in newItems set watchFolder to parent of thisFile set thisName to the name of thisFile set mainName to my stripExtension(thisName) set thisExt to the name extension of thisFile if kind of thisFile is "Macromedia Flash Movie" then -- display dialog "Is a flash source" buttons {"OK"} default button 1 set compiledName to mainName & ".swf" set foundCompiledFilesList to (every item in watchFolder whose name is compiledName) if number of items in foundCompiledFilesList is greater than 0 then -- display dialog "Compiled Version found! no more action." buttons {"OK"} default button 1 else -- display dialog "No Compiled Version" buttons {"OK"} default button 1 my publishFlash(thisFile) end if end if end repeat end tell end flashCheck to publishFlash(fileToPublish) tell application "Flash MX" activate open fileToPublish tell application "System Events" keystroke "p" using {command down, option down} end tell end tell end publishFlash -- strip extension subroutine to stripExtension(stripString) set dotChar to "." set cIndex to 0 set lastDotIndex to 0 repeat with curChar in stripString set cIndex to (cIndex + 1) if (curChar as Unicode text is equal to dotChar as Unicode text) then -- log "found dot" set lastDotIndex to cIndex -- log lastDotIndex end if end repeat set newString to (characters 1 thru (lastDotIndex - 1) of stripString as Unicode text) return newString end stripExtension
     
lapinos  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: not far from my GSM phone
Status: Offline
Reply With Quote
Feb 11, 2004, 02:03 AM
 
Great, thank you!

Do you think MX2004 reacts differently to the script? even if I correct th keystroke for the publish command, Flash does not react. the script runs completely, but Flash does not export...

I added a command to close the document window, which works, but "publish (either cmd-opt-p or shift F12) does nothing.

Thankx

greg
Lao_Tseu
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Feb 11, 2004, 05:17 AM
 
Hmm - Not sure - I don't have MX 2004.

AScript may not be able to replicate an F12 stroke.

Try changing the Keyboard shortcut for publish to something else (under the "Flash" application menu.)
     
lapinos  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: not far from my GSM phone
Status: Offline
Reply With Quote
Feb 11, 2004, 07:35 AM
 
Originally posted by Diggory Laycock:
Hmm - Not sure - I don't have MX 2004.

AScript may not be able to replicate an F12 stroke.

Try changing the Keyboard shortcut for publish to something else (under the "Flash" application menu.)
My MX2004 bugs with custom keyboard shortcuts. I tried to change the shortcut to cmd-alt-p, but it doesn't react when I invoke it; as well as all the other customised keyboard shortcuts.
I saw there is a way to call the UI components (such as item "publish" in "menu1"...) Could t be relevant in that case?
Lao_Tseu
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Feb 11, 2004, 09:04 AM
 
Yes - -good point.

I've tried to modify the script to do this, but It doesn't seem to work - It may be the way Macromedia did menus - if you can customise the keyboard shortcuts then they must have been doing something weird.

[edit] - Ha! Those crafty buggers at Macromedia put an extra space in the "publish " menu item. They can't get rid of me that easily.

Code:
--(Debugging: uncommenting the following line will allow you to test this script in Script Editor) -- flashCheck((choose file) as list) -- make this script a droplet (for testing purposes) on open droppedFiles flashCheck(droppedFiles) end open -- make this script a a Folder Action for added files on adding folder items to watchFolder after receiving newItems flashCheck(newItems) end adding folder items to -- Routine that does the checking of the "published-ness" of the source files to flashCheck(newItems) -- display dialog "folder script starts" buttons {"OK"} default button 1 tell application "Finder" repeat with thisFile in newItems set watchFolder to parent of thisFile set thisName to the name of thisFile set mainName to my stripExtension(thisName) if kind of thisFile is "Macromedia Flash Movie" then set compiledName to mainName & ".swf" set foundCompiledFilesList to (every item in watchFolder whose name is compiledName) if number of items in foundCompiledFilesList is 0 then my publishFlash(thisFile) end if end if end repeat end tell end flashCheck -- Routine to publish a flash file from source to publishFlash(fileToPublish) tell application "Flash MX" activate open fileToPublish -- tell application "System Events" to keystroke "p" using {command down, option down} end tell if not (do_menu("Flash MX", "File", "Publish ")) then -- Notice the extra space in then Publish menu item display dialog "Publish Menu Click failed!" buttons {"OK"} default button 1 end if end publishFlash -- routine for getting a filename without an extension to stripExtension(stripString) set dotChar to "." set cIndex to 0 set lastDotIndex to 0 repeat with curChar in stripString set cIndex to (cIndex + 1) if (curChar as Unicode text is equal to dotChar as Unicode text) then -- log "found dot" set lastDotIndex to cIndex -- log lastDotIndex end if end repeat set newString to (characters 1 thru (lastDotIndex - 1) of stripString as Unicode text) return newString end stripExtension -- GUI Scripting routine to click a specific menu -- Taken from http://www.apple.com/applescript/uiscripting/03.html on do_menu(app_name, menu_name, menu_item) try -- bring the target application to the front tell application app_name activate end tell tell application "System Events" tell process app_name tell menu bar 1 tell menu bar item menu_name tell menu menu_name click menu item menu_item end tell end tell end tell end tell end tell return true on error error_message return false end try end do_menu

[edit] - compulsive editing.
(Last edited by Diggory Laycock; Feb 11, 2004 at 09:28 AM. )
     
lapinos  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: not far from my GSM phone
Status: Offline
Reply With Quote
Feb 11, 2004, 09:44 AM
 
Thanks for the code (but it still skips Publish).
If I write a simple script that creates a new file and publishes it, I get a "NSReceiverEvaluationScriptError: 4" message...

I use the
click menu item "Publish" of menu bar item "File" of menu bar 1

command...

I knew Macromedia products and Apple GUI were not best friends, but this problem is really annoying.

Greg
Lao_Tseu
     
lapinos  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: not far from my GSM phone
Status: Offline
Reply With Quote
Feb 11, 2004, 09:56 AM
 
Can you reach the publish item with AS in MX 2004?
I can activate any item before and after the 3 Publish... items, but no way to call any Publish command.....

Very disappointing

greg
Lao_Tseu
     
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Feb 11, 2004, 09:58 AM
 
You can use the key code command from System Events to do this.

Make sure that you have Enable access for assistive devices checked in the Universal Access preferences in System Preferences- we haven't included an error checking to check for this. Applescripting System Events requires this.

Here is your handler for this with changes in blue.
Code:
to publishFlash(fileToPublish) tell application "Flash MX" activate open fileToPublish end tell tell application "System Events" tell process "Flash" set frontmost to true end tell key code 111 using shift down end tell end publishFlash
The keyboard shortcut on my install is shift-F12.

HTH
Craig
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Feb 11, 2004, 10:02 AM
 
Originally posted by lapinos:

I use the
click menu item "Publish" of menu bar item "File" of menu bar 1

command...
try putting a space after publish and before the closing quote.

So it's: "publish "

If that still doesn't work have a close look at the result of this script:

Code:
tell application "System Events" tell process "Flash MX" tell menu bar 1 tell menu bar item "File" tell menu "file" get UI elements end tell end tell end tell end tell end tell
and it should show the exact name of the Publish menu item.
     
lapinos  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: not far from my GSM phone
Status: Offline
Reply With Quote
Feb 11, 2004, 10:24 AM
 
Originally posted by suthercd:


Here is your handler for this with changes in blue.
Code:
to publishFlash(fileToPublish) tell application "Flash MX" activate open fileToPublish end tell tell application "System Events" tell process "Flash" set frontmost to true end tell key code 111 using shift down end tell end publishFlash
IT WORKS with both solutions.

At which frequency does the AS check for added files in the target folder?

Thanks to you two for your precious help.

Greg
Lao_Tseu
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Feb 11, 2004, 12:53 PM
 
Originally posted by lapinos:
At which frequency does the AS check for added files in the target folder?
I don't think it polls the folders - the scripts get run only when an item is added.
     
lapinos  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: not far from my GSM phone
Status: Offline
Reply With Quote
Feb 12, 2004, 02:07 AM
 
Could it be that the script runs only once and then does not "reinitialise" itself?
I activates for the first Flash file I drop, but then I have no reaction, as if the script had stopped watching the folder.....

I'll examinate that

Seeing that nice piece of utility working, I was wondering if it was possible to expand the functionality of that script, such as:
- send by iChat the swf file to selected buddies
- delete from the directory the fla and swf files

what do you think of that?

greg
Lao_Tseu
     
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Feb 12, 2004, 06:43 AM
 
The script responds when a new file is added. That is the action that triggers the script's actions. Are you saying that when you add a second file to the folder, the script's actions are not triggered?

The other functions you list for the script are possible. You should have examples of mail scripts included with your OS X install. These are a good place to start.

Look at iChat's dictionary and you will see there is a send event that is scriptable but is pretty minimal. You may have to continue to use UI scripting to accomplish that part of your plan.

Moving files to another location is straight forward scripting. Develop a script that moves one file to another folder and use that as a beginning for developing a handler to add to your Flash script that Scriptmeister Laycock wrote for you.

Craig
     
lapinos  (op)
Dedicated MacNNer
Join Date: Mar 2001
Location: not far from my GSM phone
Status: Offline
Reply With Quote
Feb 12, 2004, 07:13 AM
 
Yes - When i add a second file, the script doesn't react: nothing happens.

Does the System fail to detect the change?

Greg
Lao_Tseu
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:58 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2