Bernard Harte

Q: Format AppleScript for Forum Post?

I know there were scripts that formatted AppleScript for good presentation in a previous incarnation of these forums.

 

Pasting a script into a reply seems to have the text flowing off the left side of the display box.

 

Does anyone have either (a) a tip to prevent this or (b) a script that will help presentation please?

 

Thanks.

Posted on Oct 15, 2014 4:16 PM

Close

Q: Format AppleScript for Forum Post?

  • All replies
  • Helpful answers

  • by Niel,Helpful

    Niel Niel Oct 15, 2014 4:18 PM in response to Bernard Harte
    Level 10 (312,560 points)
    Mac OS X
    Oct 15, 2014 4:18 PM in response to Bernard Harte

    Copy and paste it into a plain text field before posting it.

     

    (114732)

  • by VikingOSX,Helpful

    VikingOSX VikingOSX Oct 15, 2014 4:22 PM in response to Bernard Harte
    Level 7 (20,809 points)
    Mac OS X
    Oct 15, 2014 4:22 PM in response to Bernard Harte

    When you open a new post, or reply, over in the upper right corner is the words, Use advanced editor. Click it.

     

    Copy and paste your compiled AppleScript from the AppleScript editor directly into this window. Select all of the script, and then under Style in the toolbar, select Paragraph. This adjusts your AppleScript code so it is no longer truncated.

     

    Of course, if there are really long lines, you may want to use the >> in the Toolbar, and pick one of the Syntax Highlighting languages to provide a horizontal scrollbar. I tend to pick Python, but plain, and others will work too.

  • by Bernard Harte,

    Bernard Harte Bernard Harte Oct 15, 2014 4:32 PM in response to VikingOSX
    Level 4 (3,309 points)
    iPhone
    Oct 15, 2014 4:32 PM in response to VikingOSX

    Thanks both.

     

    Neither is as good as the script that retained all the Script Editor indents / colour etc but now I have options.

  • by etresoft,

    etresoft etresoft Oct 15, 2014 6:07 PM in response to Bernard Harte
    Level 7 (29,173 points)
    Mac OS X
    Oct 15, 2014 6:07 PM in response to Bernard Harte

    I see what you mean. Xcode seems to do a better job...

     

    tell application "Finder"

      if folder "Applications" of startup disk exists then

      return count files in folder "Applications" of startup disk

      else

      return 0

      end if

    end tell

  • by twtwtw,

    twtwtw twtwtw Oct 15, 2014 6:37 PM in response to Bernard Harte
    Level 5 (4,935 points)
    Oct 15, 2014 6:37 PM in response to Bernard Harte

    Something I discovered: If you look at copy/pasted applescript editor text in the forum, using the HTML option, you'll see text that looks like this:

     

    <p style="text-indent: -41.6px;...

     

    basically, the forum is adding a minus sign to the text-indent property so that the indentation goes in the wrong direction. So what I've taken to doing is:

    1. copy the applescript from the Applescript Editor into the forum text box
    2. switch to HTML mode
    3. copy the html into text wrangler (or some other text editor)
    4. search for '<p style="text-indent: -' and replace it with '<p style="text-indent: ' (deleting the spurious minus sign)
    5. copy the revised html back into the forum

     

    When you do that, this:

    tell application "System Events"

      quit

    end tell

    becomes this:

    tell application "System Events"

      quit

    end tell

    works flawlessly.

  • by Pierre L.,

    Pierre L. Pierre L. Oct 21, 2014 5:10 PM in response to twtwtw
    Level 5 (4,484 points)
    Oct 21, 2014 5:10 PM in response to twtwtw

    Twtwtw, you’re a genius! However, I prefer another workaround, which imho gives a better layout:

     

    1. Copy the applescript from the AppleScript Editor into a new TextEdit document.

    2. Replace every tab with a few ordinary spaces (6 is nice).

    3. Save the TextEdit document as a Web Page (.html) on the Desktop.

    4. Double click the html document to open it in Safari.

    5. Copy the script from Safari.

    6. Paste the script into the “Full Editor” text box.

     

    When you do that, you obtain this:

     

    tell application "System Events"

         quit

    end tell

  • by baltwo,

    baltwo baltwo Oct 22, 2014 12:01 AM in response to twtwtw
    Level 9 (62,256 points)
    Oct 22, 2014 12:01 AM in response to twtwtw

    The only problem with that is if the values are too large (some of Apple's own scripts use text-indent: -39.3px and multiples of that for tabbing), which really spreads things out. So, unless you also change the values, you won't match what Script Editor shows. Also note that the line spacing is also spread out. I

     

    This is from the Script Editor window:

    Screen shot 2014-10-21 at 23.52.09 PM.png

    and this is with the minus sign removed:

     

    repeat with i from 1 to number of items in the item_list

      set this_item to item i of the item_list

      set this_item to (source_folder & this_item) as alias

      set this_info to info for this_item

      set the current_name to the name of this_info

      if folder of this_info is false and ¬

      alias of this_info is false then

      if the button_pressed is "Prefix" then

      set the new_file_name to the (the prefix_or_suffix & the current_name) as string

      else

      set the new_file_name to the (the current_name & the prefix_or_suffix) as string

      end if

      my set_item_name(this_item, the new_file_name)

      end if

    end repeat


    27" i7 iMac (Mid 2011) refurb, OS X Yo (10.10), Mavs, ML & SL, G4 450 MP w/10.5 & 9.2.2

  • by Pierre L.,

    Pierre L. Pierre L. Oct 22, 2014 7:39 AM in response to baltwo
    Level 5 (4,484 points)
    Oct 22, 2014 7:39 AM in response to baltwo

    And this is with my workaround:

     

    repeat with i from 1 to number of items in the item_list

         set this_item to item i of the item_list

         set this_item to (source_folder & this_item) as alias

         set this_info to info for this_item

         set the current_name to the name of this_info

         if folder of this_info is false and ¬

               alias of this_info is false then

               if the button_pressed is "Prefix" then

                     set the new_file_name to the (the prefix_or_suffix & the current_name) as string

               else

                     set the new_file_name to the (the current_name & the prefix_or_suffix) as string

               end if

               my set_item_name(this_item, the new_file_name)

         end if

    end repeat

  • by baltwo,

    baltwo baltwo Oct 22, 2014 12:51 PM in response to Pierre L.
    Level 9 (62,256 points)
    Oct 22, 2014 12:51 PM in response to Pierre L.

    Thanks, didn't try that, but that should do the trick.

  • by t quinn,

    t quinn t quinn Jan 26, 2015 12:51 PM in response to Bernard Harte
    Level 5 (5,027 points)
    Mac OS X
    Jan 26, 2015 12:51 PM in response to Bernard Harte

    Hi All,

     

    Hiroto, here,  has the simplest suggestion I have seen.

    Uncheck this in applescript editor preferences:

    Screen Shot 2015-01-26 at 1.43.38 PM.png

    copy and paste and you get this:

     

    activate application "Safari"

    tell application "Safari"

      tell application "System Events" to keystroke "v" using command down

    end tell


    My only complaint is that I have to unbold the rest of my comments.


    quinn