Mirza Bilal
Mirza Bilal

Reputation: 1050

Apple script for Sketch App

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

Upvotes: 3

Views: 982

Answers (1)

CRGreen
CRGreen

Reputation: 3444

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.

Upvotes: 1

Related Questions