-1

I try to check for local files (.jpg) using a plain js loop. I get an error that, for example, file 5d3eb1905b243.jpg does not exists, but actually it does.

Also, I don't understand why the else statement ins't working and why 5d3eb1905b243.jpg isn't replaced with none.jpg.

Maybe the path is the problem?

Here's the code:

if (obj[i].hasOwnProperty('sharedimage_id')) {
  var imgFile = new File(
    '/Users/student/Desktop/seminar_robobooks/archiv/img/' +
      obj[i].sharedimage_id +
      '.jpg',
  );

  if (imgFile.exists) {
    noStroke(imgFile);
    image(obj[i].sharedimage_id + '.jpg', 56.5, 70.793, 69, 42);
  } else {
    noStroke(imgFile);
    image('none.jpg', 56.5, 70.793, 69, 42);
  }
}

fabianmoronzirfas
  • 4,091
  • 4
  • 24
  • 41
VHS
  • 71
  • 3
  • 13
  • Any errors in a console? – Maksym Petrenko Aug 06 '19 at 15:20
  • 1
    Where is the code executed? – briosheje Aug 06 '19 at 15:21
  • 3
    If this runs in the browser… you can't simply access or even request arbitrary files on the user's system. That would be a serious security issue. – deceze Aug 06 '19 at 15:21
  • 2
    I don't see `File` anywhere in the [Basil.js reference](http://basiljs.ch/reference/), and it's not a standard part of JavaScript. Does InDesign provide it? If so, have you checked the documentation for it? I assume you're not trying this in the browser, but if you are, the [`File` constructor](https://developer.mozilla.org/en-US/docs/Web/API/File/File) doesn't take a path and you can't access local files from the browser. – T.J. Crowder Aug 06 '19 at 15:22
  • The code runs in InDesign CS6. `File`actually worked on another occasion. – VHS Aug 06 '19 at 15:31
  • We can't tell you if your path is a problem or not - we can't see your file structure, or your permissions. – ADyson Aug 06 '19 at 15:50
  • The path of the images is `/Users/student/Desktop/seminar_robobooks/archiv/img/`, the script is in `/Users/student/Desktop/seminar_robobooks/`. Permissions shouldn't be the problem. – VHS Aug 06 '19 at 15:58
  • `obj[i].sharedimage_id` is targeting multiple .json files. The path of the .json files is `loadString("/Users/student/Desktop/seminar_robobooks/archiv/json/"+ a + '.json');`, and this actually works. – VHS Aug 06 '19 at 16:03
  • What do you mean "targeting multiple JSON files". It's a variable, so not sure what "target" means in that context? What is the actual content of `obj[i].sharedimage_id` in the example above? And what does any of this have to do with JSON suddenly? I thought you were trying to open a jpg. It's unclear what the connection is to the JSON file. Can you clarify? – ADyson Aug 06 '19 at 17:39

1 Answers1

4

You are not providing enough context to your question.

  1. Basil.js is a InDesign Javascript/Extendscript library for generative programming. Similar to Processing and P5.js
  2. Extendscript is a Adobe flavor of Javascript (Ecmascript 3) with some additions like File or XML
  3. We can't tell if the files on your filesystem exist or the Path is right

Question: Which Basil version are you using? v2 (still in Beta)?

Below is a little more boilerplate code to keep discussing and some pointers.

  1. the noStroke does not get an argument see this
  2. the file none.jpg needs to be in the data dir next to the InDesign Document you are executing against or you need to provide a absolute path See this
  3. See this tutorial on how to handle images
var obj = [
  {
    sharedimage_id: '123456',
  },
  {
    sharedimage_id: '789012',
  },
];

for (var i = 0; i < obj.length; i++) {
  // the File API test is part of Extendscript
  // https://www.indesignjs.de/extendscriptAPI/indesign10/#File.html
  var imgFile = new File('/absolute/path/to/' + obj[i].sharedimage_id + '.jpg');

  if (imgFile.exists === true) {
    // no stroke does not take an argument
    //https://basiljs2.netlify.com/reference/color/nostroke
    noStroke();

    image(obj[i].sharedimage_id + '.jpg', 56.5, 70.793, 69, 42);
  } else {
    noStroke();
    // the image needs also a path as the one above 
    // or should be in the data dir nest to the ID doc.
    // ID doc needs to be saved for that
    // https://basiljs2.netlify.com/reference/image/image
    image('none.jpg', 56.5, 70.793, 69, 42);
  }
}

P.S. I'm one of the authors and I'm linking to the current development version of the Reference. The links might break in the future.

fabianmoronzirfas
  • 4,091
  • 4
  • 24
  • 41
  • 1
    Thank you for your advise. i created a `data` folder and everything worked just fine! – VHS Aug 08 '19 at 10:10
  • Great. Happy that it worked out. Are you using the v2 Basil version? Looks like it. – fabianmoronzirfas Aug 08 '19 at 15:21
  • No, I'm using basil.js 1.0.10 – VHS Aug 08 '19 at 17:55
  • @VHS I suggest moving on to v2. We fixed a lot of bugs and it actually is pretty stable. You can get the latest v2 here https://github.com/basiljs/basil.js/blob/develop/basil.js You can drop it next to your code and just include it with `// @include './basil.js'` – fabianmoronzirfas Aug 09 '19 at 08:33
  • Ok, thank you. By the way, I placed a question regarding InDesign scripts here [link](https://graphicdesign.stackexchange.com/questions/127524/indesign-script-targeting-specific-text-frames), maybe you can help me there? – VHS Aug 09 '19 at 08:52