applescript to remove line of code from xml file

Discussion in 'Mac Programming' started by topper99, Jan 26, 2010.

  1. topper99 macrumors newbie

    Jan 26, 2010
    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,


    -- 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"
    		-- 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
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    If the list of files is htmlFiles, then what does these_files refer to?

    My guess is that changing these_files to htmlFiles will make a big difference.

    Also, when you're processing every item in a list, the following is usually a clearer idiom:
    repeat with htmlFileBeingEdited in htmlFiles
    This form of the repeat statement automatically assigns every item in turn to the loop variable (htmlFileBeingEdited), and performs the loop body.

    For future reference, when something doesn't work, please describe exactly what happens. My guess is that no file was actually being opened, and no text was being removed, or possibly files were being overwritten with empty content. Those are just guesses based on whether or not these_files is an actual list or not.

    Finally, it seems to me you might have a much simpler script if you wrote it to tell a scriptable text-editing app to perform the actions. This is partly because the more capable apps are recordable. The way I would do this manually is the steps:
    1. Enter a Find pattern.
    2. For every file in list:
    3. Open file in document window.
    4. Find next occurrence of pattern (side-effect: entire pattern is selected)
    5. Delete current selection (qualifier: selection length non-zero, implying pattern was found).
    6. Save.
    7. Close document.
  3. topper99 thread starter macrumors newbie

    Jan 26, 2010
    Thanks for your help there. I have changed the code to now have the htmlfiles variable name instead of what it was previously. This still doesn't appear to do anything. However from the Events/Replies window i get the following-->

    tell application "Finder"
    	choose folder with prompt "Pick the folder containing the html files:"
    		--> alias "Macintosh HD:Users:Topper99:Sites:MySite:News:"
    end tell
    tell application "System Events"
    	get every file of folder "Macintosh HD:Users:Topper99:Sites:MySite:News:" whose name does not start with "." and file type = "html"
    		--> {}
    end tell
    Do the --> {} tell me that no files have been found with the particular search criteria? If so, why? The target folder is loaded with files of '.html' postfixes.

    Additionally, do you have any suggestions on a text-editing app? Is there one included in Snow Leopard which might be suitable?? (sorry for the newbie questions btw). I currently use Smultron to do html editing directly. Can i achieve a recorded script using this application or are specific API's required?

    Thanks again,

  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Correct. {} is an empty list.

    The word is "suffix" or "extension", not "postfix".

    A suffix/extension is not a file type.

    set scannable to (choose folder with prompt "Pick a folder:")
    tell application "Finder"
    	set htmlFiles to every file of scannable whose (name ends with ".html")
    end tell
    And I don't see why "System Events" should be told to perform this action. Finder is the app whose files have names and so on. Just tell Finder.

    If Smultron can show the file as plain text, then it should be fine. In Snow Leopard, the AppleScript Editor may be able to record the sequence of actions as they're performed. It's the Record button (green, contains a circle) in its toolbar. Not every app will be recordable, though, so you should try it and see what happens.

    Otherwise see what TextWrangler will do. It's free.
  5. mysterytramp macrumors 65816


    Jul 17, 2008
    TextWrangler is recordable, and it's free. Definitely a good choice for AppleScripting and text processing.


Share This Page