The API can only create tokens using images from someone's art library. (Neither art from the marketplace nor the web works.) The API has no means of searching your art library. Therefore, the only way to have the API generate a specific token is to find the URL of the image you want from your art library, which requires inspecting a token with that particular image with the API's logging capabilities. This is the script I use for doing exactly that! on('ready', function() {
on('add:graphic', function(obj) {
log(obj.get('imgsrc'));
});
});
When you drop an image on the VTT, it will log the URL for the image. Depending on where you drag the image from, this script may log the med.png/gif/jpg version of the image, and you'll have to change that to thumb.png/etc. for token creation. Once you have the URL you need, you can create some static data to save the relevant information. Here's a snippet from one of my own scripts (full URLs removed for clear reading): var bshields = bshields || {};
bshields.carddata = {
weapon: {
'Chellan, Sword of Greed': { name: 'Chellan, Sword of Greed', imgsrc: '...' },
'Karzoug\'s Burning Glaive': { name: 'Karzoug\'s Burning Glaive', imgsrc: '...' },
'Fanged Falchion': { name: 'Fanged Falchion', imgsrc: '...' } },
spell: {
'Summon Monster': { name: 'Summon Monster', imgsrc: '...' }
},
armor: {
'Snakeskin Tunic': { name: 'Snakeskin Tunic', imgsrc: '...' } }, back: '...' };
Dropping the graphic you want is then just a matter of creating a new graphic object with the appropriate properties (including the imgsrc value you've saved). Here's where that script lays out the player's hand of cards on the table: function layoutHand(index, playerid) {
var character = getCharacterForPlayer(playerid),
handPos = bshields.locations.players[index].hand,
existingCards = _.reject(findObjs({
type: 'graphic',
layer: 'objects',
controlledby: playerid
}), function(graphic) { return graphic.get('name').indexOf('freeze') >= 0; }),
existingCardNames = _.map(existingCards, function(graphic) { return graphic.get('name'); }),
fullHand = state.bshields.pfacg.charData[playerid].hand,
cardsToCreate = _.reject(fullHand, function(card) { return _.contains(existingCardNames, card.name); });
_.each(cardsToCreate, function(card) {
var newCard = createObj('graphic', {
left: handPos.left,
top: handPos.top,
width: 128,
height: 180,
imgsrc: card.imgsrc,
pageid: Campaign().get('playerpageid'),
layer: 'objects',
name: '"' + card.name + '"',
controlledby: playerid,
playersedit_name: false,
playersedit_bar1: false,
playersedit_bar2: false,
playersedit_bar3: false,
playersedit_aura1: false,
playersedit_aura2: false
});
existingCards.push(newCard);
});
existingCards = _.sortBy(existingCards, function(card) { return card.get('name'); });
_.each(existingCards, function(card, i) {
card.set({
left: handPos.left + i * 130,
top: handPos.top
});
});
}