Friday 20 June 2014

Metasequoia Script – Script Template

See this post [link] for the basics of using Metasequoia’s Script Editor.

Here is a Python script you can use as a starting point for your scripts.


 Lines that start with a hash, #, are comments in Python and are used to describe what the code is doing and not part of the executing code.

Lines of code between triple single quotes, ’’’ , are also comments.

I have listed different sections where you write the different Python structures but Python doesn’t require you do this.

For example you do not have to group all the imports together at the top. You can import on any line as long as you import a module before you use one of its functions or constants.

You should write your code in the main() function since this is the only function that is called to execute at the bottom of the code. I chose the name main() but feel free to rename it if you wish.

At the moment the main() function contains one line of code, the Python statement pass.
pass is a statement that does nothing.

After any line of Python code that ends with a colon, : , there must be at least one indented line containing some code. That is why I have used pass.

When you write your code you should remove pass and write your own code but make sure it is indented one level the same as pass.

At the top of the script I have defined a function named mqprint().

You can use this to print to the Metasequoia Script Editor output log window.

I defined the mqprint() function so I wouldn’t have to type MQSystem.println all the time.

This template should be usable in Metasequoia 3 and Metasequoia 4 but I am unable to test the script in Metasequoia 3.

In Metasequoia 3 make sure the [EOF] is the on the last line by itself and not indented.
[EOF] is not Python code but required by the Script Editor in Metasequoia 3.

Make sure when saving the script, to save the file with a .py extension.

My tabs have been converted to one space indentation in the code below.

'''
Metasequoia Python script


type a description here

'''

# imports


# global variables
doc = MQSystem.getDocument()


# function definitions
def mqprint(message):
 try:
  print(message)
 except:
  MQSystem.println(repr(message))
 return
 



# main script function
def main():
 # write your code here
 pass
 

if __name__ == "__main__":
 # clear output window
 MQSystem.clearLog()
 
 # run main function
 main()
 
 # let user know script finished
 mqprint("Script finished")



No comments:

Post a Comment