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 > Apple script help

Apple script help
Thread Tools
Fresh-Faced Recruit
Join Date: Jul 2006
Status: Offline
Reply With Quote
May 16, 2006, 10:09 PM
 
Hello all, first post even though I have been here a while. I am trying to get a certain Applescript to work with no success. I can get a repeat to work in creating new files, but on the other hand the data inside the files tends to repeat when I do not want it to. I want the numbers to be sequential in each file to correspond to the filename, but that does not seem to happen. I have posted the code below and would be glad for any help.

set thehtml to ""
set bname to "a0"
tell application "Finder"
display dialog "Value:" buttons {"OK", "Cancel"} default answer "" default button "OK"
set itemz to text returned of result
set itemza to
set header to "<!--#include virtual=\"/inc/header.inc\" -->" as string
set footer to "<!--#include virtual=\"/inc/footer.inc\" -->" as string
set start to "<div id=\"left\">
<h2>Area Rugs:</h2>
<h3><!--#ystore_catalog id=" as string
set next to " field=\"name\" --></h3>
<span class=\"image\"><a href=\"javascriptoptastic('<!--#ystore_catalog id=" as string
set nexter to " field=\"image\" -->');\"><!--#ystore_catalog id=" as string
set nexterer to "field=image format=html --></a></span><!--#ystore_catalog id="
set order to " field=caption -->
<form method=\"post\" action=\"<!--#ystore_order id=" as string
set closeup to " -->\">
<input type=\"submit\" value=\"Order\">
</form>
<br class=\"clear\" />
</div>" as string
set thehtml to thehtml & header & start & (bname & itemz) & next & (bname & itemz) & nexter & (bname & itemz) & nexterer & (bname & itemz) & order & (bname & itemz) & closeup & footer
repeat with itemz from 1 to itemz
set pat to "Mac:Users:Homeesktop:" as string
set open_file to ((pat & (bname & itemz) & ".htm") as string)
open for access (open_file) with write permission
write thehtml to (open_file as alias) as string starting at eof
close access (open_file as alias)
end repeat
end tell
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 16, 2006, 10:42 PM
 
I'm not sure exactly what you're trying to achieve, but it looks to me like you need to have the "set thehtml" line within the repeat loop. Ie, swap the "set thehtml" line and the "repeat" line.

As it is, "thehtml" is set once, just before entering the repeat loop, and then never changed, and written to each file with exactly the same content.

Also the repeat line itself doesn't make much sense in that is uses the same variable name twice. Although not syntactically wrong, I doubt it's what you really want.

Instead of:
repeat with itemz from 1 to itemz

you probably should have something like:
repeat with anItem from 1 to maxItem

Then within the loop you would reference the "anItem" variable.
     
Fresh-Faced Recruit
Join Date: Jul 2006
Status: Offline
Reply With Quote
May 16, 2006, 10:51 PM
 
Originally Posted by Brass
I'm not sure exactly what you're trying to achieve, but it looks to me like you need to have the "set thehtml" line within the repeat loop. Ie, swap the "set thehtml" line and the "repeat" line.

Also the repeat line doesn't make much sense when using the same variable name twice.

Instead of:
repeat with itemz from 1 to itemz

you probably should have something like:
repeat with anItem from 1 to maxItem

Then within the loop you would reference the "anItem" variable.
Hm... maybe I really haven't made myself clear. What I need to do is taken a number limit through input. Then I need to create a number of htm files until X. In those html files I need to input certain variables that correspond to the filename (without the extension). This needs to be done sequentially with only one instance of the code in each file.

As the script renders now, I get the correct filenames. However, the code has a01 in the first, the second has a01 and a02, the third has a01, a02 and a03. I need the second to only have a02 and the third to have only a03 etc... Does this make more sense? I made a mistake when I pasted the code and the "thehtml" variable was inside the loop. This gave me the above result I described (not the one I wanted). Thanks a lot!
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
May 17, 2006, 05:09 AM
 
How about this: (as Brass pointed out, you got the repeat a bit wrong.)

Code:
property outputDirectoryPath : "wideboy:users:diggory:desktop:testOutput:" property headerText : "<!--#include virtual=\"/inc/header.inc\" -->" property footerText : "<!--#include virtual=\"/inc/footer.inc\" -->" property startHTML : "<div id=\"left\"> <h2>Area Rugs:</h2> <h3><!--#ystore_catalog id=" property next : " field=\"name\" --></h3> <span class=\"image\"><a href=\"javascriptoptastic('<!--#ystore_catalog id=" property nexter : " field=\"image\" -->');\"><!--#ystore_catalog id=" property nexterer : "field=image format=html --></a></span><!--#ystore_catalog id=" property order : " field=caption --> <form method=\"post\" action=\"<!--#ystore_order id=" property closeupHTML : " -->\"> <input type=\"submit\" value=\"Order\"> </form> <br class=\"clear\" /> </div>" tell application "Finder" display dialog "Value:" buttons {"OK", "Cancel"} default answer "" default button "OK" set numItems to text returned of result as number repeat with currentIndex from 1 to numItems set currentFileName to "a" & currentIndex if currentIndex is less than 10 then set currentFileName to "a0" & currentIndex set thehtml to headerText & startHTML & currentFileName & next & currentFileName & nexter & currentFileName & nexterer & currentFileName & order & currentFileName & closeupHTML & footerText set open_file to ((outputDirectoryPath & currentFileName & ".htm") as string) open for access (open_file) with write permission write thehtml to (open_file as alias) as string starting at eof close access (open_file as alias) end repeat end tell

for more info on applescript repeats, see here:

http://www.macobserver.com/tips/appl...20020612.shtml
     
Fresh-Faced Recruit
Join Date: Jul 2006
Status: Offline
Reply With Quote
May 17, 2006, 07:10 AM
 
Originally Posted by Diggory Laycock
How about this: (as Brass pointed out, you got the repeat a bit wrong.)

Code:
property outputDirectoryPath : "wideboy:users:diggory:desktop:testOutput:" property headerText : "<!--#include virtual=\"/inc/header.inc\" -->" property footerText : "<!--#include virtual=\"/inc/footer.inc\" -->" property startHTML : "<div id=\"left\"> <h2>Area Rugs:</h2> <h3><!--#ystore_catalog id=" property next : " field=\"name\" --></h3> <span class=\"image\"><a href=\"javascriptoptastic('<!--#ystore_catalog id=" property nexter : " field=\"image\" -->');\"><!--#ystore_catalog id=" property nexterer : "field=image format=html --></a></span><!--#ystore_catalog id=" property order : " field=caption --> <form method=\"post\" action=\"<!--#ystore_order id=" property closeupHTML : " -->\"> <input type=\"submit\" value=\"Order\"> </form> <br class=\"clear\" /> </div>" tell application "Finder" display dialog "Value:" buttons {"OK", "Cancel"} default answer "" default button "OK" set numItems to text returned of result as number repeat with currentIndex from 1 to numItems set currentFileName to "a" & currentIndex if currentIndex is less than 10 then set currentFileName to "a0" & currentIndex set thehtml to headerText & startHTML & currentFileName & next & currentFileName & nexter & currentFileName & nexterer & currentFileName & order & currentFileName & closeupHTML & footerText set open_file to ((outputDirectoryPath & currentFileName & ".htm") as string) open for access (open_file) with write permission write thehtml to (open_file as alias) as string starting at eof close access (open_file as alias) end repeat end tell

for more info on applescript repeats, see here:

http://www.macobserver.com/tips/appl...20020612.shtml

Alright so I could kiss you! I get up this morning and and I tried out your script and it just... works! Thank you so very, very much! I also looked through the script and now understand where I got my repeat wrong. This has turned out to be a really good learning experience for me as well. I don't know sometimes I just prefer AppleScript to Automator. I tried my level best to use that, but I could not get anything done with it. Thank you so very much though again to both of you!
     
   
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 10:49 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