Educating Quotes with Automator or Applescript

Discussion in 'Mac Programming' started by Tardypigeon, Sep 14, 2014.

  1. Tardypigeon macrumors newbie

    Joined:
    Jan 1, 2014
    #1
    Hi there

    Does anyone know if it is possible to put together an application/workflow that could batch smarten quotes on a load of Textedit files? I'll probably need to do it fairly often.

    The way I see it, there seem to be two main obstacles:
    1) I don't know how to get an Automator application to take a bunch of files, open + run a workflow on them, then save + close them, then move on to the next file in the list.
    2) I can't get Automator to open a text file, smarten the quotes (which I can do manually very easily) and then save it.

    So help with either one would enable me to learn to do a load more things. I'm already fairly confident at experimenting with Automator and Applescript, but this has got me stumped.

    P.S. To clarify: Can I drop file 'SmartQuote1.txt' on an application, have it open it, find the text:

    "Help"
    'Help'
    I'm blowin' bubbles ​

    And replace it with

    “Help”
    ‘Help’
    I’m blowin’ bubbles​

    Before saving it and closing it.

    Thanks very much - have found reading through these threads very helpful in the past, hope I've pasted this in the right place, &c.

    Tardy
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    1. Try the Dispense Disk Items Incrementally action
      Info : http://www.macosxautomation.com/automator/actions.html#dispense-disk-items and http://www.peachpit.com/articles/article.aspx?p=1327771&seqNum=2

    You can always use GUI scripting but it's ugly and error prone. Here's an example :

    Code:
    set myFiles to choose file default location (path to desktop) of type {"public.rtf"} with multiple selections allowed without invisibles
    
    repeat with i from 1 to number of items in myFiles
    	set myFile to item i of myFiles
    	tell application "TextEdit"
    		activate
    		set myDocument to open myFile
    		my doGUIStuff()
    		save myDocument
    		close myDocument
    	end tell
    end repeat
    
    on doGUIStuff()
    	
    	tell application "System Events"
    		tell process "TextEdit"
    			keystroke "a" using command down
    			delay 0.7
    			if exists menu item "Show Substitutions" of menu 1 of menu item "Substitutions" of menu 1 of menu bar item "Edit" of menu bar 1 then
    				click menu item "Show Substitutions" of menu 1 of menu item "Substitutions" of menu 1 of menu bar item "Edit" of menu bar 1
    			end if
    			repeat until exists window "Substitutions"
    			end repeat
    			click button "Replace in Selection" of window "Substitutions"
    			-- Saving and closing document with keystrokes
    			--keystroke "s" using command down
    			--keystroke "w" using command down
    		end tell
    	end tell
    	
    end doGUIStuff
    
     
  3. Tardypigeon thread starter macrumors newbie

    Joined:
    Jan 1, 2014
    #3
    Thanks so much for that, Kryten! I have got that dispense incrementally thing, just never knew what it was for!

    As for the GUI scripting, I must admit I don't know where to paste that - I've tried putting it in an Automator Applescript where it returned an error message.

    But I guess automating something within a prog like TextEdit that uses menus is going to be somewhat clunky anyway.

    Weird, cause I managed to get Automator do work with Audacity in this way but it won't play ball with TextEdit!

    Thanks for your help & time - I'll mess around with the scripting side of things until something clicks.

    Tardy
     
  4. chown33, Sep 15, 2014
    Last edited: Sep 15, 2014

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Try an app other than TextEdit. For example, the free TextWrangler has an 'educate quotes' AppleScript command, which it also exposes to the GUI as a menu item. So the script will only have the single command, along with the surrounding tell and parameter handling, rather than a bunch of GUI-scripting arcana.

    http://www.barebones.com/products/textwrangler/


    As to where to put the AppleScript, you need to look up the correct form for an Automator step that contains AppleScript. The posted code isn't going to work because it's not the correct form for a workflow step. The correct form has an on run handler with two parameters.

    Google search terms:
    on run applescript workflow action

    Also look on:
    http://www.macosxautomation.com/index.html
     
  5. ArtOfWarfare, Sep 15, 2014
    Last edited: Sep 15, 2014

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    I'm feeling that neither Automator nor Applescript is right for this task. It's screaming Python at me, personally.

    Code:
    import string
    
    def smartenQuotes(s):
        # Start by learning all normal quotes in one direction.
        ret = s.replace('"', '”').replace("'", "’")
    
        # Now loop over all the characters
        for i, c in enumerate(ret):
    
            # Examine the first character and any which is immediately after whitespace.
            if i == 0 or ret[i-1] in string.whitespace:
    
                # If it's a quote facing the wrong direction, flip it.
                if c == '”':
                    ret[i] = '“'
                elif c == "’":
                    ret[i] = "‘"
        return ret
    (I haven't actually tested this function, yet... have something else to go do now but hopefully you can get it to work.)

    Applying it to files it just a matter of writing adding this to a Python script which opens a file (probably passed in through sys.argv), applies this function on the content, and then writes that file back to disk. No need to jerryrig another app to do this for you.

    Edit 2x:

    This code seems to work in Python 3, though not Python 2 on account of the unicode... not quite sure how to fix it to work on Python 2.
     
  6. Tardypigeon thread starter macrumors newbie

    Joined:
    Jan 1, 2014
    #6
    That looks amazing! So I guess Python is worth using for more text-related actions?

    So I have looked up how to open Python in Terminal, but how do I create/save the script so I can pass the files (or prefarably batch-drop them) into it? Basically, can you see a way of making it drag-and-drop rather than opening Terminal? That would be the one advantage of using Automator, because you can then save as an application.

    Thanks so much anyway!

    Tardy
     
  7. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    Type the script in a text editor (the one that comes with OS X, Text Edit, should be fine, but I'd recommend Sublime Text, personally. It has a free "trial" version with no strings attached or limitations, if you find the price tag off-putting.) Save it with the extension .py. You can run it from terminal with

    Code:
    python <name of the script file.py> <any other arguments you want to pass in, IE, file paths...>
    I've never wrapped a Python script into a GUI interface before, but if you really want to, you can probably do it with Automator, or, failing that, I often hear people mentioning Platypus for this kind of thing:
    http://sveinbjorn.org/platypus/
     
  8. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    You can use Python from Automator with the "Run Shell Script" action, just pick Python as interpreter in the drop down menu.
     

Share This Page