Macro Copier

We are always looking for new tools to create to help facilitate the table top gaming experience. Let us know if you have an idea for a new gaming tool you'd like to see. (Note: this is NOT for feature requests on existing tools!)

Moderators: dorpond, trevor, Azhrei

Post Reply
Cweord
Great Wyrm
Posts: 1343
Joined: Sun Aug 12, 2007 10:49 am
Location: Midsomer Norton, (Near Bath), UK
Contact:

Macro Copier

Post by Cweord »

I don't know how hard this will be to do, but I think it would be useful to all that have tokens with lots of macros on.

How hard would it be to write a small program that copy's all the macros from one token and pastes them to all the tokens in a directory tree? Deleting all the previous macros in the process wouldn't be a problem for me, though an option would be a nice addition for some.

That way as we get more tools in the macros we want to use on our tokens, we can update them all in one fell swoop rather than having to either remake all our tokens. I know for one that I will have to do this when the SR4 die roller hits the main code, so I am worried about creating hundreds of tokens (I want all my NPCs, Contacts, Monsters pre-set as tokens with all their stats their already) knowing that with the current situation, I am going to have to recreate them all soon.

It would also make it easier to keep our tokens up to date with all the latest additions to MT . . . .
Cweord

This message has been spell checked by Freudcheck - any mistakes are purley a figment of your imagination.
-------
My Tokens Directory
http://gallery.rptools.net/v/contrib/Cw ... er_Tokens/

User avatar
RPTroll
TheBard
Posts: 3159
Joined: Tue Mar 21, 2006 7:26 pm
Location: Austin, Tx
Contact:

Post by RPTroll »

I've got something similar in perl now. I can make changes to bring it into line with your requirements if you like. On the downside you'll need to have perl installed. If you run windows, you can use this distro. Its free.
http://www.activestate.com/Products/act ... tures.plex

I've also been futzing around with Groovy (a java-like scripting language). If you prefer Java syntax that might interface with Maptool someday. I could port it to that and it would be easier to launch file chooser dialogs and the like.

Cweord
Great Wyrm
Posts: 1343
Joined: Sun Aug 12, 2007 10:49 am
Location: Midsomer Norton, (Near Bath), UK
Contact:

Post by Cweord »

All I need to be able to do is to copy all the macros from one token, to all my other tokens, preferable in a directory tree, so I don't have to take them to one directory and sort them again.

The language it does it in makes no difference to me, my knowledge of programming languages is so out of date that I only know the very basics.

I'll install pearl quite happily

*Mimics the happy troll dance*
Cweord

This message has been spell checked by Freudcheck - any mistakes are purley a figment of your imagination.
-------
My Tokens Directory
http://gallery.rptools.net/v/contrib/Cw ... er_Tokens/

User avatar
trevor
Codeum Arcanum (RPTools Founder)
Posts: 11311
Joined: Mon Jan 09, 2006 4:16 pm
Location: Austin, Tx
Contact:

Post by trevor »

Cweord wrote: I'll install pearl quite happily
Technical historical footnote:

Larry Wall, the inventor of the Perl language had originally planned to call it "pearl", for Practical Extraction And Reporting Language. But, being a self proclaimed lazy person (hence the reason for writing the language in the first place, to automate some of his work), he dropped the "a" so that it would take less effort to write. So it's officially "Perl" without the "a".

And just like it's namesake, Perl very much takes a lazy approach to programming. Which in some cases is very convenient.
Dreaming of a 1.3 release

Cweord
Great Wyrm
Posts: 1343
Joined: Sun Aug 12, 2007 10:49 am
Location: Midsomer Norton, (Near Bath), UK
Contact:

Post by Cweord »

Lol, if it works, I ain't gonna nock it.
Cweord

This message has been spell checked by Freudcheck - any mistakes are purley a figment of your imagination.
-------
My Tokens Directory
http://gallery.rptools.net/v/contrib/Cw ... er_Tokens/

Cweord
Great Wyrm
Posts: 1343
Joined: Sun Aug 12, 2007 10:49 am
Location: Midsomer Norton, (Near Bath), UK
Contact:

Post by Cweord »

ok, I got confused just looking at the choice of distros, what is easiest and how do I do it?
Cweord

This message has been spell checked by Freudcheck - any mistakes are purley a figment of your imagination.
-------
My Tokens Directory
http://gallery.rptools.net/v/contrib/Cw ... er_Tokens/

User avatar
RPTroll
TheBard
Posts: 3159
Joined: Tue Mar 21, 2006 7:26 pm
Location: Austin, Tx
Contact:

Post by RPTroll »

Find the latest free one. :-)

I think they call it 'standard'

User avatar
RPTroll
TheBard
Posts: 3159
Joined: Tue Mar 21, 2006 7:26 pm
Location: Austin, Tx
Contact:

Post by RPTroll »

ok, here's version 0.1

Cut and paste the following code into a file named ReplaceMacros.pl
I made a special token called PCBase.rptok and NPCBase.rptok containing the macros I use for each time. The NPCBase is used for monsters as well.

You run it on windows by opening a command window and typing
perl ReplaceMacros.pl <base> <directory>

It will make a subdirectory under that called 'Remacroed' under the directory above.

I don't have a windows box to text on tonight so let me know if you have any issues. In theory Perl handles the forward slash/backslash thing for directories but I won't know until you try it. :-)

Code: Select all

#!/usr/bin/perl

# this perl script has the following usage
#  ReplaceMacros.pl <pattern> <directory>
# where <pattern> is the rptok file that contains the macros
# to be used for all rptok files in the <directory>
# the new files are stored in the <directory>/ReMacroed dir just
# in case the user wants to revert to the old files

use strict;         #makes you declare every variable to be used
use Archive::Zip;   #used to unzip and rezip the rptok files

my ($patternFile,$indir,$line,$macroTagFound,$macroEndTagFound);
my ($macros,$zip,$member1,$content,@tokenFiles,$file);
my ($newcontent,$saveXML,$trollDir);

$patternFile = shift(@ARGV);
$indir = shift(@ARGV);

if(!(-e $patternFile)) {
	print "Cannot find file $patternFile\nExiting\n";
	exit(0);
}
if(!(-e $indir)) {
	print "Cannot find file $patternFile\nExiting\n";
	exit(0);
}

print "Using $patternFile as template for *.rptok files in $indir\n";
$trollDir = $indir . "/ReMacroed";
if(-e $trollDir) {
	unlink "$trollDir/*.rptok";
} else {
	mkdir $trollDir;
}

$content = "content.xml";
$newcontent = "newcontent.xml";

$zip = Archive::Zip->new("$patternFile");
$member1 = $zip->extractMember($content);


open(IN,"$content") || die "Could not open $patternFile for input\nExiting";
$macroTagFound = 0;
$macroEndTagFound = 0;

$macros = '';
while($line = <IN>) {
	#print $line;
	$macroTagFound = 1 if($line =~ /\<macroMap>/);
	if($macroTagFound && !($macroEndTagFound)) {
		$macros .= $line;
	}
	$macroEndTagFound = 1 if($line =~ /\</macroMap>/);
}
close IN;
print "using the following macro text :\n$macros";
unlink($content);

opendir(DIR, $indir) || die "can't opendir $indir: $!";
@tokenFiles = grep { /\.rptok$/ } readdir(DIR);
closedir DIR;

foreach $file(@tokenFiles) {
	print "file = $file\n";
	$zip = Archive::Zip->new($indir . '/' . $file);
	$member1 = $zip->extractMember($content);


	open(IN,"$content") || die "Could not open $patternFile for input\nExiting";
	open(OUT,">$newcontent") || die "could not open $newcontent\nExiting";
	$macroTagFound = 0;
	$macroEndTagFound = 0;
	$saveXML = '';


	while($line = <IN>) {
		#print $line;

		if($line =~ /\<macroMap>/  && !($macroTagFound)) { 
			print "found beginning of macros with tag $line";
			$macroTagFound = 1;
			print OUT $macros; 
		}
		elsif($line =~ /\<macroMap>/) {
			print "found end of macros with tag $line"; 
			$macroTagFound = 0;
			$macroEndTagFound = 1;
		}
		elsif($macroTagFound) {
			#print "skipping macros line $line";
			$saveXML .= $line;
		} else {
			print OUT $line;
		}
	}
	# if this is a virgin rptok file (i.e. no macros)
	# you won't have an ending tag for the macros.
	# In this case the macroEndTagFound will still be zero
	# If this is the case, add the $saveXML lines back to the
	# end of the file
	if(!($macroEndTagFound)) {
		print "End Tag not found, adding the following to the end:\n$saveXML";
		print OUT $saveXML;
	}
	close IN;
	close OUT;
	$zip->removeMember("$content");
    $zip->addFile("$newcontent","$content");
    my $mtokenfile = "$trollDir/$file";
	my $status = $zip->writeToFileNamed( "$mtokenfile" );
	print "Output token file is $mtokenfile\n";
	unlink($newcontent);
	unlink($content);
}

Post Reply

Return to “Ideas for New Applications”