3

I am trying to write an Apple Script for Sketch.app (com.bohemiancoding.sketch3). What i want to do is, create some image file that can be rendered in browser from Sketch document.

And when i open Sketch.app dictionary in Script Editior i see

saveable file format enum
    Sketch : The native Sketch 2 file format
    PDF : Portable Document Format
    TIFF : Tagged Image File Format

So i thought about generating TIFF using following script, but it did not work

tell application "Sketch"
  set curdoc to document 0    
  save curdoc in "/Users/mirza/Downloads/mew2" as TIFF
end tell

I can create sketch copies in .sketch format with save command but not PDF or TIFF. Does sketch supports PDF and TIFF using apple script?

Or is there any other way around for that.

Update

I change the path to apple script format and set document index to 1. Now script looks like this

set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
    curdoc to document 1
    save curdoc in thisFilePath as TIFF -- Tried with quotes as well, gives same error
end tell

But when i run the script i got the following error

Result:
error "Sketch got an error: Can’t continue curdoc." number -1708

Update 2

Fixed typo

set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
    set curdoc to document 1
    log (path of curdoc)
    save curdoc in thisFilePath as "TIFF"
end tell

But when i run the script i got the following error

Result:
error "Sketch got an error: The document cannot be exported to the \"TIFF\" format." number -50
Mirza Bilal
  • 891
  • 11
  • 34
  • Is this the sample Sketch? It works for me. – Willeke Sep 16 '15 at 08:40
  • @Willeke Were you able to export it to TIFF? What version of Sketch are you using. – Mirza Bilal Sep 16 '15 at 10:24
  • 1
    `TIFF` is an enumerated value so it's definitely without double quotes. Try also: 1) `POSIX file "/Users/mirza/Downloads/mew2" as text` to ensure a HFS path - 2) add the .TIFF extension - 3) specify a proper file name in case `mew2` is a folder – vadian Sep 16 '15 at 10:36
  • @vadian True TIFF is an enum, but when i give it without quotes it says `error "The variable TIFF is not defined." number -2753 from "TIFF".` Even after adding `as text` and `.tiff` i am still getting this error `error "Sketch got an error: The document cannot be exported to the \"TIFF\" format." number -50` – Mirza Bilal Sep 16 '15 at 10:53
  • are you using the code in an enclosing application tell block of a different application? If yes there might be an terminology clash, then move the Sketch part out of the other application tell block – vadian Sep 16 '15 at 10:58
  • Yes i am using it between `tell application "Sketch"` and `end tell`. What is the other way around for doing this. – Mirza Bilal Sep 16 '15 at 11:03
  • Sketch.app version 3.3.3 does not have what you wrote as being in the AS dictionary. It has this: save v : Save an object. save specifier : the object for the command [as text] : The file type in which to save the data. [in alias] : The file in which to save the object. – CRGreen Sep 16 '15 at 14:27
  • @Willeke, you said this works for you? What works? What version of Sketch? – CRGreen Sep 16 '15 at 15:05

1 Answers1

1

There are a number of things wrong with your code, but, to start with, you're going to find it hard to get definitive answers using software that isn't available anymore. Sketch has been at version 3 for a while now, and the AppleScript dictionary has probably changed. That being said, here are some thoughts about your code:

If that is what the Sketch 2 AS dictionary reads, then the AS functionality has changed in v3. I'd like to help, but I can't find v2 anywhere, so I can only do this in the dark.

set thisFilePath to choose file name--use this to select a new file;
------- a Mac AppleScript path is returned (a file specification,
------- actually, which is different from a string or alias
------- (but an alias is kind of like a file spec)
tell application "Sketch"
    set curdoc to document 1--not zero-based; 1 is frontmost doc
    save curdoc in thisFilePath as "TIFF"--*this is a guess
end tell

So, I don't know what that last save line will do, but it might work. In Sketch 3, "TIFF" format isn't allowed in saving, but it does have an as parameter as part of the save, which is supposed to be paired with a text string representing the format (like "TIFF", above). Sketch 2 seems to have a different scheme (the parameter with as is not a string). If I save without the as parameter in Sketch 3, it saves in Sketch's native format. So you might try this without quotes (like you have). I'm just doing what the v3 dictionary tells me to do. Here are a couple of solutions and tips:

  1. document 1 should work to reference the frontmost document;

  2. If you want for some reason to write out your path using POSIX (like you've done), you can use

    POSIX file "/Users/mirza/Downloads/mew2"

to return an AppleScript's Mac-style path, which is of this form:

"yourHardDriveName:Users:mirza:Downloads:new2"

You can also get what I have here as "yourHardDriveHame:" by doing

tell application "Finder" to set sDr to startup disk as string

then concat by doing

sDr & "Users:mirza:Downloads:new2"

You can also do

tell application "Finder" to set myHome to home as string

which should return the Mac-style path to the home folder. (And yes, there are other paths Finder allows you to get, too).

There's some stuff to play with.

CRGreen
  • 3,406
  • 1
  • 14
  • 24
  • I am using sketch3 not sketch2, please check updated question. – Mirza Bilal Sep 16 '15 at 10:14
  • 1
    you can easily specify the HFS path of the user's download folder directly with `path to downloads folder as text` – vadian Sep 16 '15 at 10:18
  • @vadian Path is not an issue, but the issue is to export it to TIFF – Mirza Bilal Sep 16 '15 at 10:28
  • 1
    I know, it's just for convenience – vadian Sep 16 '15 at 10:37
  • As I mentioned in my answer, Sketch doesn't allow saving as TIFF. Also, I noticed that it has a weird way of doing exports (not by scripting) whereby it makes a slice and expects it to be saved as a png. I don't think doing this by scripting can work at all. Best to tell the developers that their scripting needs to be improved to include export. – CRGreen Sep 16 '15 at 14:36
  • 1
    ...but keep in mind that they clearly don't care very much about AppleScript; Cocoascript is their thing now, which is powerful but has a serious learning curve -- AND they phased out an earlier language so there are non-working examples all over the place. – CRGreen Sep 16 '15 at 15:26
  • 1
    I should clarify that Sketch has export options galore, including TIFF, but not by AppleScript. By hand or by Cocoascript is the way to do it. – CRGreen Sep 16 '15 at 15:28