Table Support with Apple Script?
  • Hi,

    would it be possible to add a basic table support to insert the HTML code for a simple table via Apple Script?

    Ideal it would be when there would be an option to set the number of rows and columns with the script. Could you give me a simple start up script how to do this? Then I might be able to extend it also to give more options like width, height, borders etc...

    Many thanks!

    Yogie
  • Hi Yogie - here's an example of a simple script to do something along these lines. It will prompt for dimensions of the table and then generate HTML, inserting it into the frontmost MarsEdit document.


    on InsertHTML(theHTML)
    tell application \"MarsEdit3.3.5\"
    tell document 1
    set selected text to theHTML
    end tell
    end tell
    end InsertHTML

    -- Example to create a simple table
    set tableWidth to (text returned of (display dialog \"How many colums?\" default answer 3)) as number
    set tableHeight to (text returned of (display dialog \"How many rows?\" default answer 3)) as integer

    -- Generate some HTML to form a default table with tbose dimensions
    set tableSource to \"<table border='1'>\"
    repeat with thisRow from 1 to tableHeight
    set tableSource to tableSource & \"<tr>\"
    repeat with thisColumn from 1 to tableWidth
    set defaultCellText to thisRow & \"x\" & thisColumn
    set tableSource to tableSource & \"<td>\" & defaultCellText & \"</td>\"
    end repeat
    set tableSource to tableSource & \"</tr>\"
    end repeat

    set tableSource to tableSource & \"</table>\"

    InsertHTML(tableSource)
  • Hello Daniel,

    many thanks for the script, but I'm getting an error when I run the script from the menu ...

    Error Number:Can’t make end into type reference.
    -1700

    Any idea? Many thanks in advance!

    When I start the script from the script editor it does it's work very well!

    Cheers
    Yogie
  • Looks like I had an extra, unnecessary "end" in the script. I've edited it above to fix the problem. It seems to work now from the MarsEdit script menu.
  • Wow this is great, I have adjusted the script a bit for my needs and the outcome is this, I have now three versions of it, one is the script above with giving the option to define the border, the second is to give a specific height and width in percentage and the last one is giving a table the height and width in pixels. So for all who are interested...

    my simple table script ...
    -start-
    on InsertHTML(theHTML)
    tell application \"MarsEdit\"
    tell document 1
    set selected text to theHTML
    end tell
    end tell
    end InsertHTML

    -- Example to create a simple table
    set tableColumns to (text returned of (display dialog \"How many colums?\" default answer 3)) as integer
    set tableRows to (text returned of (display dialog \"How many rows?\" default answer 3)) as integer
    set tableBorder to (text returned of (display dialog \"Border width?\" default answer 0)) as integer


    -- Generate some HTML to form a default table with tbose dimensions
    set tableSource to \"<table border='\" & tableBorder & \"'>\"
    repeat with thisRow from 1 to tableRows
    set tableSource to tableSource & \"<tr>\"
    repeat with thisColumn from 1 to tableColumns
    set defaultCellText to thisRow & \"x\" & thisColumn
    set tableSource to tableSource & \"<td>\" & defaultCellText & \"</td>\"
    end repeat
    set tableSource to tableSource & \"</tr>\"
    end repeat
    set tableSource to tableSource & \"</table>\"

    InsertHTML(tableSource)

    -end-

    my table with % as dimension

    -start-
    on InsertHTML(theHTML)
    tell application \"MarsEdit\"
    tell document 1
    set selected text to theHTML
    end tell
    end tell
    end InsertHTML

    -- Example to create a simple table
    set tableColumns to (text returned of (display dialog \"How many colums?\" default answer 3)) as integer
    set tableRows to (text returned of (display dialog \"How many rows?\" default answer 3)) as integer
    set tableBorder to (text returned of (display dialog \"Border width?\" default answer 0)) as integer
    set tableWidth to (text returned of (display dialog \"Table width? (%)\" default answer 100)) as integer
    set tableHeight to (text returned of (display dialog \"Table height? (%)\" default answer 100)) as integer

    -- Generate some HTML to form a default table with tbose dimensions
    set tableSource to \"<table border='\" & tableBorder & \"' width='\" & tableWidth & \"%' height='\" & tableHeight & \"%'>\"
    repeat with thisRow from 1 to tableRows
    set tableSource to tableSource & \"<tr>\"
    repeat with thisColumn from 1 to tableColumns
    set defaultCellText to thisRow & \"x\" & thisColumn
    set tableSource to tableSource & \"<td>\" & defaultCellText & \"</td>\"
    end repeat
    set tableSource to tableSource & \"</tr>\"
    end repeat
    set tableSource to tableSource & \"</table>\"

    InsertHTML(tableSource)

    -end-

    my table with px as dimension

    -start-
    on InsertHTML(theHTML)
    tell application \"MarsEdit\"
    tell document 1
    set selected text to theHTML
    end tell
    end tell
    end InsertHTML

    -- Example to create a simple table
    set tableColumns to (text returned of (display dialog \"How many colums?\" default answer 3)) as integer
    set tableRows to (text returned of (display dialog \"How many rows?\" default answer 3)) as integer
    set tableBorder to (text returned of (display dialog \"Border width?\" default answer 0)) as integer
    set tableWidth to (text returned of (display dialog \"Table width? (px)\" default answer 100)) as integer
    set tableHeight to (text returned of (display dialog \"Table height? (px)\" default answer 100)) as integer

    -- Generate some HTML to form a default table with tbose dimensions
    set tableSource to \"<table border='\" & tableBorder & \"' width='\" & tableWidth & \"px' height='\" & tableHeight & \"px'>\"
    repeat with thisRow from 1 to tableRows
    set tableSource to tableSource & \"<tr>\"
    repeat with thisColumn from 1 to tableColumns
    set defaultCellText to thisRow & \"x\" & thisColumn
    set tableSource to tableSource & \"<td>\" & defaultCellText & \"</td>\"
    end repeat
    set tableSource to tableSource & \"</tr>\"
    end repeat
    set tableSource to tableSource & \"</table>\"

    InsertHTML(tableSource)

    -end-

    I really gonna love MarsEdit ;-)

    There are still some things I would like to add to the table scripts, like alignment of the cells etc. but for now I'm happy, feel free to include the scripts with your next release :-) well the main part of it is your work anyway ...
  • Ok here is another script version where it all comes to one ;-)


    on InsertHTML(theHTML)
    tell application \"MarsEdit\"
    tell document 1
    set selected text to theHTML
    end tell
    end tell
    end InsertHTML

    -- collect some figures to build the table
    set tableColumns to (text returned of (display dialog \"How many colums?\" default answer 3)) as integer
    set tableRows to (text returned of (display dialog \"How many rows?\" default answer 3)) as integer
    set tableBorder to (text returned of (display dialog \"Border width? (for no border choose 0)\" default answer 1)) as integer
    set measure to (text returned of (display dialog \"Width and Height in ... ? (%, px, etc.)\" default answer \"%\")) as string
    set tableWidth to (text returned of (display dialog \"Table width? (\" & measure & \")\" default answer 50)) as integer
    set tableHeight to (text returned of (display dialog \"Table height? (\" & measure & \")\" default answer 50)) as integer
    set align to (text returned of (display dialog \"Cell Align? (left, center, right, justify)\" default answer \"left\")) as string
    set valign to (text returned of (display dialog \"Cell vAlign (top, middle, bottom, baseline)\" default answer \"top\")) as string

    -- Generate some HTML to form a table with collected figures
    set tableSource to \"<table border='\" & tableBorder & \"' width='\" & tableWidth & measure & \"' height='\" & tableHeight & measure & \"'>\"
    repeat with thisRow from 1 to tableRows
    set tableSource to tableSource & \"<tr>\"
    repeat with thisColumn from 1 to tableColumns
    set defaultCellText to thisRow & \"x\" & thisColumn
    set tableSource to tableSource & \"<td align='\" & align & \"' valign='\" & valign & \"'>\" & defaultCellText & \"</td>\"
    end repeat
    set tableSource to tableSource & \"</tr>\"
    end repeat
    set tableSource to tableSource & \"</table>\"

    InsertHTML(tableSource)


    Hope you like it!?

    Cheers
    Yogie
  • Awesome! I am excited to see you taking such great advantage of MarsEdit's scriptability. At some point I want to open up an official "plugin" model which would allow folks like yourself to make for instance a "Tables Plugin" to use with MarsEdit.
  • Hi, Daniel,
    I'm learning to use MarsEdit and would like to try making tables using the scripting you've been discussing here with Yogie.
    I've made one small custom script already to insert a link with an added 'target="_blank"' so that a reader clicking it retains the page containing the link.
    I tried simply copying the script text above into the "Opening markup" field for a new script but the results when I try it out appear to be that my blog page gets the script text inserted into it rather than a table.
    I'm guessing that the script perhaps needs to wind up inside a pair or some such but I'm not seeing how to do it.
    Can you help me with this?
    Thanks!
  • Hi there BlogrOneAtAstuteKid - I think you are probably trying to paste the AppleScript above into MarsEdit's formatting macro editor. Unfortunately the macros don't support AppleScript within the macros themselves. The example Yogie posted is working outside the scope of the formatting macros, automatically inserting HTML via MarsEdit's general scriptability. To run a script like this you would need to use e.g. Apple's own AppleScript editor, or something like my FastScripts utility (http://www.red-sweater.com/fastscripts).

    If you haven't used AppleScript before it's a bit of a learning curve but you can really do some powerful things with it.

    Daniel
Start a New Discussion

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!