Run VectorWorks' script from the clipboard

VectorWorks is a CAD application with limited AppleScript support. It has it’s own scripting language, VectorScript, based on Pascal. The language is quite powerful and indeed beloved by many VectorWorks users. Through the AppleScript DoScript command it is possible to run a VectorScript code.

I think this little script might be of use to fellow VectorScripters.
I suggest you to keep it in your Library Scripts Folder, in order to have full access to it.
I use it for testing on the fly script snippets from online lists. If you are reading this, I guess you might also have this habit.

Copy a valid VectorScript snippet, for example this simple line:
alrtDialog(‘Hey! I was called by AppleScript!’);

Then run the following:

-- Orso.b.schmid 2006
-- run a VectorScript from the clipboard

tell application "VectorWorks"
	activate
	try
		-- extract clean text from the clipboard
		DoScript («class ktxt» of ((the clipboard as string) as record))
	on error errorMessage
		display dialog errorMessage
		-- something went wrong with string coercion
	end try
end tell

If your clipboard content wasn’t a valid string, you get an AppleScript error.
If your script snippet wasn’t a valid VectorScript, you get a VectorWorks error.

The main point is to clean the clipboard text from it’s style attributes. The VectorScript compiler sends an error, if the text is styled. This is done thru the record conversion, then picking only the needed portion of text string, dropping it’s styles.

If you want to experiment a little, try copying a string somewhere in your browser window, then run this:

set text item delimiters to return
display dialog clipboard info as text
set text item delimiters to ""

Do you have a “Styled Clipboard Text” somewhere?
I bet you do.

Now try this:

set text item delimiters to return
-- now we overwrite the clipboard with it's own data as string
set the clipboard to the clipboard as string
display dialog clipboard info as text
set text item delimiters to ""

Do you have a “Styled Clipboard Text” still there?
Most likely.

Now we try passing the clipboard data to a var, this should settle the matter, you think?

set text item delimiters to return
-- now we set the clipboard to string
set theClip to the clipboard as string
set the clipboard to theClip as string as text as list as text as list as record -- really trying hard
display dialog (clipboard info) as text
set text item delimiters to ""

This is kind of unexpected, but the styles are still there: they survived all coercions.

Turning the data into a record allows extracting the clean text out of it. It’s the «class ksty», hyding hideously everywhere.
Mostly it’s a great feature of AppleScript, not loosing styles even across variables.
In this case it’s a pain.

Orso B. Schmid

Model: PoweBook g4, 1500
AppleScript: 1.9.3
Browser: Safari 312.6
Operating System: Mac OS X (10.3.9)

While I don’t use VectorWorks myself, Orso, I’m sure your advice will be very helpful to those who do. :slight_smile:

As you’ve demonstrated, coercing Unicode text to string actually produces styled text (although it has a class of string) - and the style data can cause problems for an application that doesn’t know how to handle it.

If the clipboard contains multiple items, and at least one of the items is text, the “highest quality” text item will be returned. Unicode text is preferred over styled text which is preferred over plain text. However, since plain text is required here, a way is needed to extract only this form from those available.

There are a few ways to do this, one being the «class ktxt» extraction method that you suggested. This takes the highest quality text item copied to the clipboard and coerces it to string. (If Unicode text, then the coercion will produce styled text. If styled text, the coercion will have no effect.) The result is then coerced to a record - usually with «class ktxt» (plain text) and «class ksty» (style data) properties - from which the «class ktxt» property can then be finally extracted.

Another method might be to coerce the clipboard itself to a record - and then to extract the string property from that. So, after copying some relevant text:

(the clipboard as record)'s string

The result can be demonstrated using the same technique that you tested with. After copying some text:

set the clipboard to (the clipboard as record)'s string
clipboard info
--> {{string, x}} [where x = the length of the copied text]

I see you joined us quite recently - so welcome. Impressive post. :smiley:

I thank you very much! Much more elegant!
Are there other methods you know of?

Orso

Sure, Orso. There was a discussion a while back, Failed coercion to URL, that compared a few methods.

That’s all very interesting!
I guess I have been silly to enter the forum so late.

Orso B. Schmid

Not at all, Orso. (I, too, discovered this site much later than I should have done!) Better late than not at all… :wink:

Thanks. the information has been very helpful.

I have a question, I think the answer is supposed to be very simple, but I just don’t see it.

I’ve been trying to run vectorscript from applescript. How can I do that?

I have a vectorscript file called line.text and line.vss.

I would like to run them from applescript.

I used

tell application "VectorWorks"
	activate
	try
		DoScript line		
	on error errorMessage
		display dialog errorMessage
	
	end try
end tell

but it doesnt work.

Can someone help me?

Thank you

You must load the text contained in the file through AppleScript. It can be done through open for access or passing through a text editor. Then the approach depends on which editor you are using.
Below an example for TextWrangler, which is my editor. You can use it for BBedit exactly as it is, I think. Just let the compiler map to BBedit when/if it asks you to locate the app.

I’ll post a batch runner for VectorScript here as soon as I have a little time.

Orso

tell application "TextWrangler"
	activate
	-- here you load the text content
	set theScript to text of window 1 as text
	
	try
		tell application "VectorWorks"
			activate
			-- sometimes one wants to keep a command window open long.
			with timeout of 360 seconds
				DoScript theScript
			end timeout
		end tell
	on error
		-- an error would always be related to AppleScript, NEVER to VS.
		display dialog "Something went wrong with your text file!"
	end try
end tell

Model: PowerBook g4, 1500
AppleScript: 1.9.3
Browser: Safari 312.6
Operating System: Mac OS X (10.3.9)

THank you for the help Orso,

I still got an error message when I copy and paste your code.
I also use TextWrangler

DoScript theScript
→ What do I need to put as theScript, Is it the file name of my Vectorscript file?
that’s what I did and I still get an error message

I don’t know what do I do wrong?

The previous AS loads the content of the frontmost TextWrangler window into the var theScript. Then the var theScript is passed as argument to the DoScript command.

You should save the previous AppleScript into the ~/Library/Application Support/TextWrangler/Scripts/ folder. Then it’s accessable in the scripts menu of TW.
Also quite comfortable is to set up a key for the AppleScript file: supposing you saved the applescript under the name “Run VectorScript” in the scripts folder of TW, check windows → palettes → scripts. A list of all installed AppleScripts (if any) pops up. Select “Run VectorScript” and press “set key”. A little window appears: enter a key combination. So you can launch the script much easier. From the frontmost TW window.

Please mind that this technic is well only for commands. VectorScripts making use of parametrical records like PIOs and Tools rather come off well with INCLUDES. This is altogether another approach.
This is not the forum for discussing this: check http://www.orangedust.co.uk/vs_folder/vscolour.html for an excellent example about includes. There you can also download my TextWrangler language module for syntax colouring including the undocumented routines, if you don’t have it already.

Enjoy and welcome to the addictive VS scripting (which scripting is not addictive?..) :slight_smile:

Orso

Thanks for the help. Sorry, But I actualy don’t understand most of it.

What I’m trying to do is I’m trying to run a vectorscript file from applescript. I want to do an integration automation of scripts. I may call different vectorscript file from applescript.

How can I do that?

Thank you very much Orso, for your help.

Ajoso, I didn’t forget to answer you. I just discovered that VectorWorks 12 is not properly recognizing the Close command any longer. :mad:
Please check this thread Very simple Batch Run VectorWorks script http://bbs.applescript.net/viewtopic.php?pid=62637#p62637
There you find a simple script for doing what you wish: load a script file.

I hope it helps you.
Please open a thread on the forum if you need more informations.

Orso