Please Help with Text Wrangler

Discussion in 'Mac Apps and Mac App Store' started by etnad, Sep 23, 2012.

  1. etnad macrumors newbie

    Joined:
    Sep 23, 2012
    #1
    I am new to the forum I hope this is the correct one to post ...:confused:


    Is there an Applescript I can use in text wrangler to select a folder and
    1) zap gremlins in every file.
    2) Save every file with specific txt enchoding (example Latin 9) and whichever Carriage return?
    I really like Text wrangler and I use the great AS recording facilities but I am a newbie in AS
    If anyone could help it will be great
    Regards and thanks again
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
  3. etnad, Sep 23, 2012
    Last edited: Sep 23, 2012

    etnad thread starter macrumors newbie

    Joined:
    Sep 23, 2012
    #3
    the first script doesn't work unfortunately.
    Looking at Text Wrangler Applescript dictionary the syntax seems different, however I am not particulary skilled in AS therefore unable to understand how to fix the script.

    As for Camelot reply, I did not mention I was talking about "text" files as I asked help for TextWrangler BBedit which only work with text files, else I would have stated the various documents I was trying to work with had they been of various kind. "Zap Gremlins" is an option only available in BBedit or TextWrangler. There might be other programs performing the same operation, however googling "Zap Gremlins" returns only BBedit files types.

    I posted this question here hoping to find some help.

    Regards and thanks.
     
  4. kryten2, Sep 23, 2012
    Last edited: Sep 24, 2012

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Can you post an example txt file to work with? Thanks. You can zip it and attach it to your post.

    Try this :

    Code:
    set targetFolder to choose folder
    set destinationFolder to choose folder
    
    tell application "Finder"
    	repeat with i from 1 to count files of folder targetFolder
    		set aFile to file i of targetFolder
    		set fileName to name of aFile
    		set savePath to (destinationFolder as string) & fileName
    		tell application "TextWrangler"
    			set aFile to aFile as alias
    			open aFile with LF translation
    			zap gremlins of text 1 of window 1 with nulls
    			zap gremlins of text 1 of window 1 with controls and non ASCII characters
    			set encoding of text document 1 to "Western (ISO Latin 9)"
    			set line breaks of text document 1 to Mac
    			save text document 1 to file savePath
    			close text document 1
    		end tell
    	end repeat
    	tell application "TextWrangler" to quit
    end tell
    Some output of the event log :

    tell application "TextWrangler"
    open alias "SnowLeopard:Users:kryten:Desktop:doc:testdocument3.txt" with LF translation
    --> text document 1
    zap gremlins text 1 of window 1 with nulls
    --> "b°°°€€"
    zap gremlins text 1 of window 1 with controls and non ASCII characters
    --> "b"
    set encoding of text document 1 to "Western (ISO Latin 9)"
    set line breaks of text document 1 to Mac
    save text document 1 to file "SnowLeopard:Users:kryten:Desktop:doc:testdocument3.txt"
    close text document 1
    quit
    end tell

    Properties of modified file :

    {container:application "TextWrangler", id:5, modified:false, modifiable:true, modification date:date "Monday 24 September 2012 20:53:23", name:"testdocument3.txt", on disk:true, file:file "SnowLeopard:Users:kryten:Desktop:doc:testdocument3.txt", creator type:"!Rch", window:project window 1 of application "TextWrangler", container web site:missing value, text encoding:"Western (ISO Latin 9)", FTP Info:missing value, is FTP:false, line breaks:Mac, source language:"0†_Säê
     
  5. etnad thread starter macrumors newbie

    Joined:
    Sep 23, 2012
    #5
    I didn't try yet your script:
    Thanks anyway
    here is a short example of my files

    I will try your script and inform you

    Regards and thanks
     

    Attached Files:

  6. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    Before and after :
     

    Attached Files:

  7. etnad thread starter macrumors newbie

    Joined:
    Sep 23, 2012
    #7
    Thanks and regards for now.

    However there is a huge variety of different encodings even for the same languages. - especially in Chinese, but also in Russian vs. Serbian or Ukranian - From the kids I get messages in their languages but in various flavours.
    In other words:
    While they always fill up a UTF-8 form in I created for their contacts and homework. If they fill in the form from Browser A it might use a specific Chinese or Greek encoding system.
    In your script as in the previous often Carriage return, vanish as typing in certain browsers, or through their own website provider, a CR key may return a linefeed instead.
    I wonder if there is a way to actually zap the gremblins getting the codes, as it can be done manually in Text Wrangler.
    Basically my work is as follows:
    A working script loads the UTF-8 files from my server. Another script converts them to plain text. With the script you kindly gave me often I loose the CR and everything becomes one very long line and various controls vanish.
    I was thinking that zapping into code (Example \0x201C for double quotes, or \0x2028 for linefeed) will be quite handy, as often \0x2028 replaces CR in many text I get.
    In the AS doctionary for TextWRANGLER I read:

    zap gremlins v : Removes or replaces unwanted characters from text
    zap gremlins specifier : the text to be processed

    [zap action delete_gremlin/replace_with_code/replace_with_char] : action to be performed on gremlin characters (defaults to delete_gremlin)

    [zap character text] : character to use as the replacement with “replace_with_char” is used

    [nulls boolean] : zap null (ASCII 0) characters?

    [controls boolean] : zap control characters?

    [non ASCII characters boolean] : zap non-ASCII characters?
    → text : the modified text



    I just dont know how to change these lines in your script to get the codes as from the dictionary:

    zap gremlins of text 1 of window 1 with nulls
    zap gremlins of text 1 of window 1 with controls and non ASCII characters

    with something like ap gremlins of text 1 of window 1 with nulls replace_with_char

    So another script would allow me to fix the various grmlins reading the codes(example) to CR if they are line feeds, or double quotes or semicolon and so on

    Thanks again

    Merci or thanks
     
  8. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    This is the best I can offer you for the moment.

    Code:
    set targetFolder to choose folder with prompt "Choose the target folder"
    set destinationFolder to choose folder with prompt "Choose the destination folder"
    
    tell application "Finder"
    	repeat with i from 1 to count files of folder targetFolder
    		set aFile to file i of targetFolder
    		set fileName to name of aFile
    		set fileNameExt to name extension of aFile
    		set savePath to (destinationFolder as string) & fileName
    		if fileNameExt is "txt" then
    			tell application "TextWrangler"
    				--activate
    				set aFile to aFile as alias
    				open aFile with LF translation
    				zap gremlins text 1 of text document 1 zap action replace_with_code with non ASCII characters and controls
    				replace "\\0x2028" using "\n" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				replace "\\0x300A" using "(" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				replace "\\0x300B" using ")" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				replace "\\0xFF1A" using ":" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				replace "\\0xFFFC" using "" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				replace "\\0x201C" using "\"" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				replace "\\0x201D" using "\"" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				replace "\\0x3000" using "\n" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				replace "\\0x3002" using "" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				replace "\\0x25A0" using "" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
    				set theEncoding to text returned of (display dialog "What encoding would you like to set for file " & fileName with title "File encoding" default answer "Western (ISO Latin 9)")
    				set encoding of text document 1 to theEncoding
    				set lineBreaks to text returned of (display dialog "What line breaks would you like to set for file " & fileName & return & "Example : Mac/Unix/DOS" with title "File line breaks" default answer "Unix")
    				set line breaks of text document 1 to lineBreaks
    				save text document 1 to file savePath
    				close text document 1
    			end tell
    		end if
    	end repeat
    	tell application "TextWrangler" to quit
    end tell
     

Share This Page