I'm a long time reader, first time writer so let me start off by saying hi to all.
I have been trying to write an apple script to remove a line of code that appears in the iweb html files before I can upload them to my web server.
The string i want to remove is pointed to with 'ptr' and I intend to search for the root folder then extract all html files, edit them by removing the 'ptr' string and then save them back as their original filename and extension.
I have had a go but something seems to not want to work. Can someone assist me with what I've written?
Kind regards,
Topper99
I have been trying to write an apple script to remove a line of code that appears in the iweb html files before I can upload them to my web server.
The string i want to remove is pointed to with 'ptr' and I intend to search for the root folder then extract all html files, edit them by removing the 'ptr' string and then save them back as their original filename and extension.
I have had a go but something seems to not want to work. Can someone assist me with what I've written?
Kind regards,
Topper99
Code:
-- Find string
set ptr to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
-- Open the finder and find all html files pointed
-- to by the users folder selection
tell application "Finder"
try
-- Get the folder containing the .html files
set rootFolder to (choose folder with prompt "Pick the folder containing the html files:") as string
-- In the background, find all files of .html type
tell application "System Events"
set htmlFiles to every file of folder rootFolder whose name does not start with "." and (file type is "html")
end tell
-- For all .html files found...
repeat with i from 1 to the count of htmlFiles
set htmlFileBeingEdited to (item i of these_files)
-- Open the file to write to...
open for access htmlFileBeingEdited with write permission
-- extract the PHP lines we need removing
set outputCode to extractPhpCode(htmlFileBeingEdited, "?>")
-- clear the current contents of the file
set eof of htmlFileBeingEdited to 0
-- write the file and repeat for other files until finished
write outputCode to htmlFileBeingEdited as string
-- close the file thus saving it.
close access htmlFileBeingEdited
end repeat
on error
display dialog "no files found"
end try
end tell
to extractPhpCode(source, separator)
set tid to AppleScript's text item delimiters -- save them for later
set AppleScript's text item delimiters to separator -- find the first part
set htmlCode to text of text item -1 of source -- everything after the first
set AppleScript's text item delimiters to tid -- bac to original values
return htmlCode -- pass back the remaining html code
end extractPhpCode