Very simple Batch Run VectorWorks script

This script is meant as follow up to the thread Run VectorWorks’ script from the clipboard http://bbs.applescript.net/viewtopic.php?id=16735.

VectorWorks is a CAD application with its own script language, VectorScript. It is a powerful script language allowing access to almost any element of the application, but it has one limit: VectorScript can -under normal conditions- only operate on the frontmost open window.
The topic on the previously mentioned thread evolved to a very common question among VectorScripters: how to run a VectorScript on multiple files. This question is highly “recursive” since years among VectorScripters. Since to my knowledge no free scripts of this sort are available online, I thought, well, it is time to put out one. And it has to be a simple one.

For testing, copy whatever object in your drawing (for example a symbol containing drawing informations), save the VectorScript snippet below in a text file, then to run the batch runner on some chosen files:

{ SAVE THIS IN A TEXT FILE }
{ paste in place clipboard content on every sheet layer }
PROCEDURE pasteInEverySheetLayer;
VAR
layerHand :HANDLE;
temp_i : INTEGER;

BEGIN
layerHand := FLayer;
WHILE LayerHand <> NiL DO BEGIN
IF (getObjectVariableInt(LayerHand, 154) = 2) THEN BEGIN
Layer(getLName(LayerHand));
DoMenuTextByName(‘Paste In Place’, 0);
END;
LayerHand := NextObj(LayerHand);
END;
END;
Run(pasteInEverySheetLayer);

-- very simple batch runner for VectorScript Files
-- Orso B. Schmid 2006
-- MacOs X.3+

-- NOTE: the close command seams to be broken under VW 12.
-- Avoid using this script under VW 12
-- Please avoid having two versions of VW contemporarily open,
-- this can lead to unpredictable results

-- change file types according to your needs
global VWfileTypes
set VWfileTypes to {"DO11"} -- VectorWorks 11

-- choose a Text file containing a VectorScript code
-- You better have that code valid, VW won't tell AppleScript he dislikes it...
set aVWscript to readFile((choose file with prompt "Choose a VectorScript file " of type "TEXT") as text)

-- choose one or more VectorWorks files 
set theChosenFiles to choose file with prompt "Choose one or more vectorWorks files " of type VWfileTypes with multiple selections allowed without invisibles
launch application "VectorWorks"

set theProcessedFiles to {return}

repeat with aVWfile in the theChosenFiles
	runTheScript(aVWfile, aVWscript)
	set end of theProcessedFiles to name of (info for aVWfile) & return
end repeat
display dialog "Processed files: " & theProcessedFiles as text

on runTheScript(theVWfile, theVSscript)
	tell application "VectorWorks"
		open theVWfile
		
		ignoring application responses
			DoScript theVSscript
			
			-- this deals with eventual dialogs raised by VectorScript
			-- according to the busy state of VW, this might fail miserably
			-- I just put it here to show how to access the problem
			-- couldn't find any working way to put it on a "if" condition
			-- nor seams to be possible to trap a vectorScript taking more time as 1 sec.
			
			-- the whole system events is kept busy by the eventual VS dialog
			-- turn on "Enable access for assistive devices" in the "Universal Access" pane for this to work
			
			tell application "System Events"
				tell process "VectorWorks"
					set frontmost to true
					delay 1
					key code 36
				end tell
			end tell
			
		end ignoring
		close document 1 saving yes -- this is broken in VW 12 !!!!!!!
	end tell
end runTheScript

-- reads the values stored in the script file
to readFile(aFilePath)
	try
		set openFile to open for access file aFilePath without write permission
		set theVWdata to read openFile as text -- this has to be clean text, unstyled
		
		close access openFile
	on error errorMessage
		try
			close access openFile
		end try
		set theVWdata to ""
		display dialog errorMessage
	end try
	
	return theVWdata
end readFile


The script allows a very simple batch running of a VectorScript file on any number of chosen VectorWorks files.
The script must be a valid VectorScript: no error will be sent by VectoWorks to AppleScript if the script for some reasons do not compile. Eventual dialogs raised by the VectorScript are all confirmed. Please consider this carefully.

Also remember that the AppleScript implementation of VectorWorks is reduced to very few commands (open, close, translate, doScript) and never, under no conditions, does VectorWorks outputs a result. The efficiency of your scripting relies thus on the efficiency of your VectorScript.

I hardly think an explanation of this AppleScript is needed, since it is of most basic nature:
let a user choose a script file,
let a user choose a list of VectorWorks files
load the script file into a variable through the standard additions open for access and read,
run the script in the list of chosen files.

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

I don’t understand some part of the script.

set theChosenFiles to choose file with prompt "Choose one or more vectorWorks files " of type VWfileTypes with multiple selections allowed without invisibles

For that line, what is the VWfileTypes?

I’m not sure what to look for, I couldn’t find file with VW type.

Thanks Orso

The file type is defined in the global declaration at beginning of the script:

global VWfileTypes
set VWfileTypes to {"DO11"} -- VectorWorks 11

The different versions of VectorWorks all have different file types. What for a Version of VectorWorks do you use? The topic of avoiding opening wrong files is not so marginal as you might think. The parsing has to be done outside VW, since the application won’t help you in the least once he gets its paws over a file.
Supposing you would like to open VectorWorks files from MiniCad 6 up to VectorWorks 12, you could set VWfileTypes to this list:

set VWfileTypes to {"MC6d", "MC7d", "MC8d", "MC9d", "DO11", "DO12"}.

(I actually forgot what’s the type for VW10)
Since we are batch running a script we rather limit the choice of the user to only the files that can really be processed.
If you try to open a file from a version newer than your current VectorWorks, you get a VectorWorks error. If you try to open a file which is older, VectorWorks will ask you if you want to convert it.
In both cases you get a dialog, which we rather avoid in our batch runner.

Should you wish to take the possible conversion into account, so you should add to the open command the optional parameter “notifying no” or try to trap the files before running through VW and then treat them with a “translate” in if condition.
The parameter “notifying no” simply translates the file without prompting for a dialog. Please mind that I didn’t try it since a long time. I cannot grant that it functions as promised.

tell application "VectorWorks"
       open theVWfile notifying no
      -- do something

If you are uncertain what for a file type your currently active VectorWorks is producing, there are a some ways to find out. Depending on the contest of your script, you might prefer one over the oher, here a couple of common use:
Supposing you have a whatever file named aVWfile on your desktop, if you select it:

tell application "Finder" to display dialog (file type of (selection as alias) as string)

otherwise more generally:

set theFile to (((path to desktop folder) as string) & "aVWfile.mcd") as alias

tell application "Finder" to get file type of theFile -- using the Finder
file type of (info for theFile) -- using scripting additions

All together accessing the different versions of VectorWorks gets a little complicated if you need to switch application version. Even accessing the application by id doesn’t always work, if you have two versions contemporarily open, like any vectorScripter do.
Below a snippet for anybody interested in accessing different VW versions.

to runVSfile(aVWfile, aVSscript)
	local aLocVWapp
	
	tell application "Finder"
		set docPapa to file creator of (info for aVWfile)
		set aLocVWapp to application file id (docPapa) as string
	end tell
	
	using terms from application "VectorWorks"
		tell application aLocVWapp
			open aVWfile notifying no
			-- notifying no: this avoids dialogs if the files are converted from an older version
			DoScript aVSscript
			if docPapa is not "VW12" then close document 1 saving yes -- this is broken in VW 12 !!!!!!!
		end tell
	end using terms from
end runVSfile

Orso, it has been very helpful. It works.

But I have another question, If I don’t want to have the pop up button to ask for the vectorworks file and vectorscript file. How do I change the script? I want to preset the vectorworks file and vectorscript file, such that the end user doesn’t need to choose anything.

Thank you again …

Ah, Ajoso, I think I’ll leave that to you as exercise! :stuck_out_tongue:
Search for “Droplet” into this forum. You’ll need some listing and filtering of the dropped files, should they be more than one or should some be of wrong or mixed type. All in all is a nice easy exercise. You have now all elements to fulfill it and you’ll please your end user. ;). If the end user is general you must use the second level tell block for addressing VectorWorks. Please remember -but you no doubt do the same- that VectorScripter tend to have many VW version installed for test purposes.
Enjoy!

Please switch to the AppleScript X forum for further discussions.

Orso

Still related to the batch runner

I tried to run the applescript file which you posted in java.

import com.apple.cocoa.foundation.;
import com.apple.cocoa.application.
;
import com.apple.cocoa.application.NSApplication;
//import NSApplication;

public class partB {
public static void main(String[] args)
{
NSApplication.sharedApplication();

	 String script = "global VWfileTypes \n"
	+"set VWfileTypes to {\"MC6d\", \"MC7d\", \"MC8d\", \"MC9d\", \"DO11\", \"DO12\"} \n"
	+"set aVWscript to readFile((choose file with prompt \"Choose a VectorScript file \" of type \"TEXT\") as text) \n"
	+"set theChosenFiles to choose file with prompt \"Choose one or more vectorWorks files \" of type VWfileTypes with multiple selections allowed without invisibles \n" 
	+"launch application \"VectorWorks\" \n"
	+"set theProcessedFiles to {return} \n"
	+"repeat with aVWfile in the theChosenFiles \n"
	+"runTheScript(aVWfile, aVWscript) \n"
	+"set end of theProcessedFiles to name of (info for aVWfile) & return \n"
	+"end repeat \n"
	+"display dialog \"Processed files: \" & theProcessedFiles as text \n"
	+"on runTheScript(theVWfile, theVSscript) \n"
	+"tell application \"VectorWorks\" \n"
	+"open theVWfile \n"
	+"ignoring application responses \n"
	+"DoScript theVSscript \n"
	+"tell application \"System Events\" \n"
	+"tell process \"VectorWorks\" \n"
	+"set frontmost to true \n"
	+"delay 1 \n"
	+"key code 36 \n"
	+"end tell \n"
	+"end tell \n"
		
	+"end ignoring \n"
	+"close document 1 saving yes \n" 
	+"end tell \n"
	+"end runTheScript \n";


	 NSAppleScript myScript = new NSAppleScript(script);
	 NSMutableDictionary errors = new NSMutableDictionary();
	 System.out.println("Yay AppleScript!");
 }

}

I tried to run your applescript batch runner from java. But it doesn’t work.
I hope someone can let me know, what I do wrong.

Thanks in advance

Sorry to keep on posting here, since it’s still related to batch runner.