I recently got a mac-mini and have some programming background in Visual Basic. I built a website over the weekend with iWeb and posted it easily to Apple’s .mac site. However, when I tried to post it to my ISP, it wouldn’t work because many of the filenames that I used and the folder names that iWeb created had spaces in them, which are not accepted by most web hosts.
In order to finally get it to upload and work on my ISP’s webserver I had to manually change all of the spaces in the filenames and folder names to an “" character. Then I had to manually edit dozens of html and javascript files to change the references to the folder and filenames - replacing the “%20” occurrences in the files with "”.
With a little planning ahead, I can eliminate the spaces in the filenames, but iWeb still creates folder names with spaces. After doing some research into Applescript, I believe that it has the capability to automate this process. While trying to solve this problem I discovered that there are many people out there that are dealing with this same issue and a script of this type would be very useful to many others in addition to myself.
Here’s what the script I’m looking for would need to do:
-
Run when the user drops a website folder (with all of it’s associated files, folders, and sub-folders) into a “FixIt” folder.
-
Recursively parse the folder tree and change all spaces to “_” characters for all folder names.
-
Recursively parse the folder tree to locate each “html” and “js” file, and find/replace all occurrences of “%20” within each file with the “_” character.
I would love to write this script and claim credit for it, but being whatever comes before you become a newbie to Applescript, it would probably take more time and patience than I have. My feeble attempts so far have resulted in some nasty errors and the mysterious disappearance of all the files and folders in my “Sites” directory.
Anyone up to the challenge?
Thanks for your input!
Model: mac-mini
Browser: Firefox 2.0.0.3
Operating System: Mac OS X (10.4)
Pretty cool idea and not overly difficult. If you could provide me with a real or sample site folder to work with to test against that would help. Shoot me a PM or email through the forum here if you would like.
You realise that if you have a space in a menu name i.e. About me, My Photos then iWeb will create the folders as About me , My Photos but if you change them to About_me, My_Photos
then iWeb will create the folders as About_me, My_Photos
So I’m not sure a script is really warranted. Just add the underscore’s in iWeb and re-export
Mark,
Yes, I realize the the folder names correspond to the menu item names. Changing the names to something like Photo_Album fixes the folder name issue, but it makes the website look kind of cheezy to have underscores in the items that appear in the navigation bar. I guess I want to have my cake and eat it too!
James,
I’ll send you a sample file shortly.
Thanx guys
I see your point
that would wind me up just looking at it if it was my pages.
So there is nothing wrong with having cake and eating it… 
Thanks for sending over the sample. I worked this up and it worked in a small test site I created. Let me know if it works for you or if you run into any problems =)
on run
set txt_ext_list to {"html", "htm", "xml", "css", "js"}
set site_folder to choose folder with prompt "Please select the site folder for cleanup"
tell application "Finder"
set element_list to entire contents of site_folder as alias list
repeat with an_element in element_list
set element_name to name of an_element
if element_name contains "%20" then
set name of an_element to my SaR(element_name, "%20", "-")
end if
if name extension of an_element is in txt_ext_list then
my processTextFile(an_element)
end if
end repeat
end tell
end run
to SaR(sourceText, findText, replaceText)
set OTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to findText
set tempText to text items of sourceText
set AppleScript's text item delimiters to replaceText
set sourceText to tempText as string
set AppleScript's text item delimiters to OTID
return sourceText
end SaR
to processTextFile(file_path)
try
close access file_path
end try
set file_data to read file_path
if file_data contains "%20" then
set new_file_data to my SaR(file_data, "%20", "-")
set file_ref to (open for access file_path with write permission)
set eof file_ref to 0
write new_file_data to file_ref
close access file_ref
end if
end processTextFile
James,
With a few minor modifications, your script worked like a charm! All I had to do was publish my iWeb site to it’s own folder (since the site includes a “root” folder with all the files and subfolders in it as well as an index.html file), run your script and point it to the folder where the site was published, then wait a minute or so for it to do it’s job. The website works perfectly on my local disk. Next I’ll try uploading it.
If it’s ok with you I’d like to include a “How-To” page on my site and include the code for your script - giving you credit of course!
I had to change the FindText for folder and filenames and changed the ReplaceText from “-” to “_” for filenames, foldernames, and the modified code inside the files . I also added a dialog to let the user know it’s finished since it takes a minute or so to run.
Here’s the script with the mods:
on run
set txt_ext_list to {"html", "xml", "css", "js"}
set site_folder to choose folder with prompt "Please select the iWeb site folder to convert"
tell application "Finder"
set element_list to entire contents of site_folder as alias list
repeat with an_element in element_list
set element_name to name of an_element
if element_name contains " " then
set name of an_element to my SaR(element_name, " ", "_")
end if
if name extension of an_element is in txt_ext_list then
my processTextFile(an_element)
end if
end repeat
end tell
display dialog "The iWeb site folder has been converted. Would you like to view the folder?" buttons {"No", "Yes"}
if button returned of result = "Yes" then
tell application "Finder" to open the site_folder
end if
end run
to SaR(sourceText, findText, replaceText)
set OTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to findText
set tempText to text items of sourceText
set AppleScript's text item delimiters to replaceText
set sourceText to tempText as string
set AppleScript's text item delimiters to OTID
return sourceText
end SaR
to processTextFile(file_path)
try
close access file_path
end try
set file_data to read file_path
if file_data contains "%20" then
set new_file_data to my SaR(file_data, "%20", "_")
set file_ref to (open for access file_path with write permission)
set eof file_ref to 0
write new_file_data to file_ref
close access file_ref
end if
end processTextFile
Thanks again for such a quick and helpful response!
Glad I could lend a hand Mark and feel free to post your “How to” =)
Let me know if you need any additional help!