set html to read file (desktopPath & "html.txt")
Sorry, it fell at the first hurdle. Can’t find the file.
Can we use something like:
choose file "html.txt"
To find the file manually then assign its contents to a variable?
set html to read file (desktopPath & "html.txt")
Sorry, it fell at the first hurdle. Can’t find the file.
Can we use something like:
choose file "html.txt"
To find the file manually then assign its contents to a variable?
yes, of course, replace the two lines with
set html to choose file with prompt "choose the html file" without invisibles
set java to choose file with prompt "choose the javascript file" without invisibles
OK, I ended up with this. It seems to work! I need to test it a bit more. I’ll be back.
(I tried your choose file method but I couldn’t get it to put the text into the variables).
set desktopPath to path to desktop as Unicode text
say "select file java one"
choose file "java1.txt"
set repText to read result
-- the text to replace after BODY
choose file "html.txt"
set html to read result
say "select file java two"
choose file "java2.txt"
set java2 to read result
-- insert the javascript before </HEAD>
set {TID, text item delimiters} to {text item delimiters, "</HEAD>"}
set {i1, i2} to text items of html
set html to java2 & i2
set text item delimiters to TID
set html to i1 & html
-- replace the string after <BODY (inclusive >) with repText
set text item delimiters to "<BODY "
set {i1, i2} to text items of html
set text item delimiters to TID
set html to i1 & "<BODY " & repText & text ((offset of ">" in i2) + 1) thru -1 of i2 --*)
-- save the file as htmltest.txt on desktop
set ff to open for access file (desktopPath & "htmltest.txt") with write permission
write html to ff
close access ff
sorry, I forgot the read command
this works
set html to read (choose file with prompt "choose the html file" without invisibles)
set java to read (choose file with prompt "choose the javascript file" without invisibles)
Excellent, thanks! Can I embed the Javascript texts. In other words can I set a variable to each of them without having to read in the files? It won’t mess up the line breaks in the Javascript will it?
So the final result will be:
set desktopPath to path to desktop as Unicode text
-- the text to replace after BODY
set html to read (choose file with prompt "choose the html file" without invisibles)
set java2 to "lots of lines
of javascript text
here"
set repText to "lots of lines
of javascript text
here"
-- insert the javascript before </HEAD>
set {TID, text item delimiters} to {text item delimiters, "</HEAD>"}
set {i1, i2} to text items of html
set html to java2 & i2
set text item delimiters to TID
set html to i1 & html
-- replace the string after <BODY (inclusive >) with repText
set text item delimiters to "<BODY "
set {i1, i2} to text items of html
set text item delimiters to TID
set html to i1 & "<BODY " & repText & text ((offset of ">" in i2) + 1) thru -1 of i2 --*)
-- save the file as htmltest.txt on desktop
set ff to open for access file (desktopPath & "htmltest.txt") with write permission
write html to ff
close access ff
Oh, another question: does it matter if the HTML filename has an “html” extension?
(Stefan, ich bedanke mich Ihnen! Wie darf ich Ihnen bezahlen?)
yes, I used the files only for example.
btw, you can add a line break to a string with
"teststring" & ASCII character 10
you’re welcome
A bit of a tangent, but…
If you find yourself doing this sort of thing alot, and are already doing web work, you might want to look into BBEdit anyway. Since it has GREP searches built-in and can search-and-replace in entire directories of files, it excels at this sort of thing…without the scripting.
I’ve done similar tasks to what you just described on web sites large and small…quite a bit when I was heavy into web design. Was a great way to make sweeping changes to a site when the client didn’t have PHP or databse-driven functionality.
Not saying an AppleScript won’t work, mind you, just proposing a different type of solution,.
I did try BBEdit-Lite but it didn’t seem to have an AppleScript library.
Anyway, Stefan’s script works a treat and I can almost understand it.
I don’t have a need for further scripting right now, although I am very interested in manipulating text in documents.
I’m accustomed to handling $trings with “InterBase” language on the BBC Microcomputer. Applescript is a whole new world and seems very user-unfriendly by comparison. I need to get to grips with user text entry. In my day that would be “GET$”. Lord knows how Applescript does it!
Martin
Hi Martin,
It’s more than one word but it fits into one line
set Get_Dollar to text returned of (display dialog "Please enter text:" default answer "Hello World")
Actually, I was proposing not scripting, since it only takes a few dialog boxes within BBEdit itself to make the functionality work, especially if you understand GREP.
(I know, I know, sacrilege!)
Also, BBEdit (dunno about the Lite version) is entirely scriptable, one of the most scriptable apps I know of (so is MailSmith for anyone who cares, made by the same company). Also rare among Mac apps these days, BBEdit is Recordable too–an invaluable tool for figuring out how to script an application.
So of course, you could Record BBEdit doing what you want, then write a script with that framework.
Besides, I’m a bit of a purist, I figure any Mac web developer really needs BBEdit in the toolkit anyway.
Even since I stopped heavy web development I find BBEdit an invaluable text manipulation tool in any case.
Okay, okay, off my soapbox…
What I’m actually doing is making a “pop-up” (actually a “Hover Advert”) for my various web sites to get people to sign up to my mailing list. There’s lots of Windoze programmes to do this but not a single one for OSX.
Now that it’s working, I’m thinking that I ought to offer it for maybe $5 a shot to other Mac users who, like me, want a hover ad but don’t know Javascript nor have time/skill to learn it.
If I’m going to do this, I need to refine it a little. For a start, I need to let them select a colour for the background. At the moment this is inserted into the HTML file as bgcolor=“#FFFF00”
Is it possible, at the end of Stefan’s script, to bring up a color swatch and get the user to select a colour which is then used to replace the “#FFFF00” in the HTML file?
This would be rather neat but if it’s impossible/ difficult I guess I’ll settle for Stefan’s suggestion:
set Get_Dollar to text returned of (display dialog "Please enter color:" default answer "FFFF00")
I feel guilty about asking for help now that I’ve decided to “go commercial” but I’m happy to pay for any assistance. (FWIW I already donated $10 to this forum before started this thread - so I do believe in paying my way).
What think you? Was denken sie darauf? Qu’y pensez-vous ? ¿Qué piensa usted en ello?
Oh, you can see the “hover ad” working at any of my three satcure sites. satcure.com .net .co.uk
Martin
Edit: sorry, Calvin, I did read your post! Agree in principle. I have many “tools” like “SubEthaEdit”, “Can Opener” and BBEdit Lite but I never needed the full version.
A color swatch would be nice but maybe I should crawl before I walk. Can you complete the color code insertion in this, please?
set desktopPath to path to desktop as Unicode text
say "choose file file one"
set repText to read (choose file with prompt "Choose file1" without invisibles)
-- the text to replace after <BODY......
say "choose file file two"
set java2 to read (choose file with prompt "Choose file2" without invisibles)
say "choose web page file"
set html to read (choose file with prompt "Choose web page file" without invisibles)
-- insert the javascript before </HEAD>
set {TID, text item delimiters} to {text item delimiters, "</HEAD>"}
set {i1, i2} to text items of html
set html to java2 & i2
set text item delimiters to TID
set html to i1 & html
-- replace the string after <BODY (inclusive >) with repText
set text item delimiters to "<BODY "
set {i1, i2} to text items of html
set text item delimiters to TID
set html to i1 & "<BODY " & repText & text ((offset of ">" in i2) + 1) thru -1 of i2 --*)
--Ask for the colour code
set Get_Dollar to text returned of (display dialog "Please enter color:" default answer "FFFF00")
-- insert the color code
set (every word where it is "#HJFRKC") to "#" & Get_Dollar
-- I don't think that is quite right!
-- save the file as newpage.htm on desktop
set ff to open for access file (desktopPath & "newpage.htm") with write permission
write html to ff
close access ff
say "saving file htmltest to your desk top"
Hi Martin,
Unfortunately the syntax of AppleScript is quite different from Interbase
AppleScript has a built-in option to choose a color.
I changed the script, to pick the color and replace the two ‘FONT COLOR=“#FF0000”’ terms
with the chosen color but not the BGCOLOR term.
set desktopPath to path to desktop as Unicode text
say "choose file file one"
set repText to read (choose file with prompt "Choose file1" without invisibles)
-- the text to replace after <BODY......
say "choose file file two"
set java2 to read (choose file with prompt "Choose file2" without invisibles)
say "choose web page file"
set html to read (choose file with prompt "Choose web page file" without invisibles)
-- insert the javascript before </HEAD>
set {TID, text item delimiters} to {text item delimiters, "</HEAD>"}
set {i1, i2} to text items of html
set html to java2 & i2
set text item delimiters to TID
set html to i1 & html
-- replace the string after <BODY (inclusive >) with repText
set text item delimiters to "<BODY "
set {i1, i2} to text items of html
set text item delimiters to TID
set html to i1 & "<BODY " & repText & text ((offset of ">" in i2) + 1) thru -1 of i2 --*)
set colorList to choose color
set htmlColor to ""
repeat with i in colorList
set htmlColor to htmlColor & text -2 thru -1 of ("0" & (do shell script "perl -e 'printf(\"%X\", " & i div 256 & ")'"))
end repeat
set {TID, text item delimiters} to {text item delimiters, "\"#"}
set j to text items of html
set text item delimiters to TID
set html to item 1 of j
repeat with i from 2 to ((count j) - 1)
set html to html & "\"#" & (htmlColor as string) & text (offset of quote in (item i of j)) thru -1 of item i of j
end repeat
set html to html & "\"#" & last item of j
set ff to open for access file (desktopPath & "newpage.htm") with write permission
write html to ff
close access ff
say "saving file htmltest to your desk top"
Notes: the technique to search and replace in AppleScript is to split the text by a given search string (the text item delimiter)
into a list of items, change the text item delimiter to the replace string an concatenate the text items again.
In the color panel, use the web palette, the calculation of the value is not “waterproof”
Thanks, Stefan, but I don’t understand how you are replacing HJFRKC in the line:
This is contained in the JS code that is inserted into the HTML page.
(I made up the “HJFRKC”. It could be anything but I wanted to minimize the risk of replacing a real color that was specified in the HTML page for something else, such as the page background color.)
Edit: I tried your script and it changes the PAGE background color! I just want to change the TABLE color, please.
I apologize if I explained it badly. Vielleicht soll ich auf deutsch schreiben.
set {TID, text item delimiters} to {text item delimiters, "HJFRKC"}
BTW I think the color swatch is brilliant! You make it seem so easy. I thought it would take pages of code.
Yep, nearly there except I didn’t realize that the color has to be replaced in two places.
I guess it’s just a matter of running that replace routine twice. Could you do that for me, please?
[X] |
|
this code replaces any occurence of “#HJFRKC” in the text
Oops, you are correct!
I had forgotten to put the first “#HJFRKC” in my test file. :rolleyes:
Hooray! Thanks. You can invoice me now. I can pay via Paypal or, if you prefer, I’ll make another donation to this forum.
Martin, I don’t want to get paid for just helping somebody. I’m glad, everything works fine for you.
Stefan, you are very kind but there’s a possibility I could earn a little money from this!
OK, I’ll make another donation to the forum. I would hate for it to go under from lack of funds!
I may want to do a bit more with this script so I hope you’ll be patient with me if I revisit it.