Get image filename of NPC token

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Lej
Kobold
Posts: 6
Joined: Mon Aug 20, 2012 3:00 am

Get image filename of NPC token

Post by Lej »

Is it possible to get the filename (e.g. "troll.png", or at least "troll") of the image used by a NPC token?

Akodo Makama
Giant
Posts: 249
Joined: Mon Apr 20, 2009 9:31 pm

Re: Get image filename of NPC token

Post by Akodo Makama »

When an image is added to the campaign, it is turned into a token asset, which is stored by it's MD5 checksum. The campaign file only references these asset numbers. The original name is no longer available.

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Get image filename of NPC token

Post by Azhrei »

It's correct that there is no way for the user to retrieve or see the image name.

However, the name is actually still stored in the data structure, IIRC. I don't remember why, except it might have to do with situations where the resource has to be loaded and it fails (network error, for example) so the error message can include the name.

Since there's no way to get to that information I suppose this is moot. :|

tripwire
Kobold
Posts: 2
Joined: Thu Jul 25, 2013 1:02 am

Re: Get image filename of NPC token

Post by tripwire »

Not helpful within MapTool (or Windows...), but I've made a quick and dirty shell script that extracts and restores the original image filenames stored in .rptok and .cmpgn files. I've used it to pull token state images from campaign files.

Extract the .rptok/.cmpgn file. From inside the 'assets' folder, run this:

Code: Select all

#!/bin/bash
for file in *
do
  test -f "$file" || continue
  if [[ "$(file -ib $file)" != text* ]]
  then
    continue
  fi

  name=`grep "<name>.*</name>" $file | sed 's/.*>\(.*\)<.*/\1/'`
  ext=`grep "<extension>.*</extension>" $file | sed 's/.*>\(.*\)<.*/\1/'`
  mv $file.$ext "$name.$ext"
done

Lej
Kobold
Posts: 6
Joined: Mon Aug 20, 2012 3:00 am

Re: Get image filename of NPC token

Post by Lej »

Is it possible to have a macro that is run automatically every time a new token is created?

If so, it is possible to set maptool to use the filename as the name for new tokens. The macro could store the filename (i.e. the token name) and then rename the token.

The idea is to create a simple "identify creature" macro. It should change the name from "Unknown Creature" to, for example, "Cave Troll" (where Cave Troll is the name of the image used by the token). I could of course enter the names by hand, the question is if it's somehow possible to avoid that.

User avatar
jfrazierjr
Deity
Posts: 5176
Joined: Tue Sep 11, 2007 7:31 pm

Re: Get image filename of NPC token

Post by jfrazierjr »

tripwire wrote:Not helpful within MapTool (or Windows...), but I've made a quick and dirty shell script that extracts and restores the original image filenames stored in .rptok and .cmpgn files. I've used it to pull token state images from campaign files.

Extract the .rptok/.cmpgn file. From inside the 'assets' folder, run this:

Code: Select all

#!/bin/bash
for file in *
do
  test -f "$file" || continue
  if [[ "$(file -ib $file)" != text* ]]
  then
    continue
  fi

  name=`grep "<name>.*</name>" $file | sed 's/.*>\(.*\)<.*/\1/'`
  ext=`grep "<extension>.*</extension>" $file | sed 's/.*>\(.*\)<.*/\1/'`
  mv $file.$ext "$name.$ext"
done
Well. the script could be run in Windows, but the user would FIRST have to download a bash shell. IIRC, Cygwin install has that as an option(and I think it's defaulted on), but it's been a LONG time since I installed it and don't use it but once in a great while. of course, then the user would have to spend enough time learning how to actually get around in the shell if they are not already familiar with windows commandline(which most windows users are not... :roll: )
I save all my Campaign Files to DropBox. Not only can I access a campaign file from pretty much any OS that will run Maptool(Win,OSX, linux), but each file is versioned, so if something goes crazy wild, I can always roll back to a previous version of the same file.

Get your Dropbox 2GB via my referral link, and as a bonus, I get an extra 250 MB of space. Even if you don't don't use my link, I still enthusiastically recommend Dropbox..

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Get image filename of NPC token

Post by Azhrei »

Welcome to RPTools!

It's always great to see a fellow Unix user. :)
tripwire wrote:

Code: Select all

#!/bin/bash
for file in *
do
  test -f "$file" || continue
  if [[ "$(file -ib $file)" != text* ]]
  then
    continue
  fi

  name=`grep "<name>.*</name>" $file | sed 's/.*>\(.*\)<.*/\1/'`
  ext=`grep "<extension>.*</extension>" $file | sed 's/.*>\(.*\)<.*/\1/'`
  mv $file.$ext "$name.$ext"
done
Good, but there's a lot of extra work going on there! You can change the two variable assignments to these lines. Note that there's only a single sed command and only a single command substitution. This requires one fork() and one exec() while the original code required six fork()s and four exec()s. You'll see a huge performance difference when there's a large number of files. :)

Code: Select all

set -- $(sed -n \
    -e 's#.*<name>\(.*\)</name>.*$#\1#p' \
    -e 's#.*<extension>\(.*\)</extension>.*$#\1#p' )
name=$1
ext=$2
I noticed you used [[ ]] for your test condition (excellent!) and mostly used double quotes where they were required (they're only missing in the mv command and the two grep's). Both of those are things that shell script beginners don't do correctly. (I see a lot of that 'cuz I teach shell scripting classes.)

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Get image filename of NPC token

Post by Azhrei »

Lej wrote:Is it possible to have a macro that is run automatically every time a new token is created?
No.
If so, it is possible to set maptool to use the filename as the name for new tokens. The macro could store the filename (i.e. the token name) and then rename the token.
This could be done using a statsheet macro, which would be triggered the first time anyone moused over the token.

Post Reply

Return to “Macros”