\n within regex gets replaced with new line by AppleScript Editor

Discussion in 'Mac Programming' started by poobear, Jun 27, 2012.

  1. poobear
    Expand Collapse
    macrumors 6502a

    poobear

    Joined:
    Nov 20, 2009
    #1
    I am trying to set up an applescript that removes all the empty lines from my clipboard. I came up with this script

    Code:
    do shell script "pbpaste | sed 's/^\n//' | pbcopy"
    I have tried the regex s/^\n// with a perl one-liner and it works fine.

    However, every time I try to compile the applescript in the AppleScript Editor I get

    Code:
    do shell script "pbpaste | sed 's/^
    //' | pbcopy"
    The \n is removed and replaced with a blank line, and the script will not work.

    How do I get this to work? :confused:

    Thanks
     
  2. chown33
    Expand Collapse
    macrumors 604

    Joined:
    Aug 9, 2009
  3. poobear
    Expand Collapse
    thread starter macrumors 6502a

    poobear

    Joined:
    Nov 20, 2009
    #3
    The script seems to do nothing when I do \\n.

    (To try, copy something with a blank line, add the script to AppleScript Editor and run it, and then paste your clipboard and see if the blank line is gone)
     
  4. kryten2
    Expand Collapse
    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Go to preferences-->Editing and check Escape tabs and line breaks in strings
     

    Attached Files:

  5. poobear
    Expand Collapse
    thread starter macrumors 6502a

    poobear

    Joined:
    Nov 20, 2009
    #5
    Thanks that did the trick!

    Although, for some reason my clipboard is empty after running the script, makes no sense. Oh well another night at google
     
  6. kryten2
    Expand Collapse
    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    Try this :

    Code:
    pbpaste | sed '/^$/d' | pbcopy
     

    Attached Files:

  7. poobear
    Expand Collapse
    thread starter macrumors 6502a

    poobear

    Joined:
    Nov 20, 2009
    #7
    kryten2 I love you.
     
  8. kryten2
    Expand Collapse
    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium

Share This Page