New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getSelectedDocument returns undefined when sketch app is not focused #253

Closed
nicutimofte opened this Issue Sep 23, 2018 · 7 comments

Comments

5 participants
@nicutimofte
Copy link

nicutimofte commented Sep 23, 2018

I'm working on a plugin which has an event triggered from outside of the sketch app - the action is not done in sketch.
I'm using getSelectedDocument api and it returns undefined whenever the sketch app is not focused - notice that the document is open.

Any suggestion / workaround to solve this issue?
Thanks!

@mathieudutour

This comment has been minimized.

Copy link
Contributor

mathieudutour commented Sep 23, 2018

You can try

const document = sketch.Document.fromNative(
  context.actionContext && context.actionContext.document
        ? context.actionContext.document
        : context.document
 )

as a workaround.

I’ll fix this for the new Sketch version

@nicutimofte

This comment has been minimized.

Copy link
Author

nicutimofte commented Sep 23, 2018

I forgot to mention that there's no access to context. The event is not a sketch action. Actually, it can be seen as a function invocation.

@mathieudutour

This comment has been minimized.

Copy link
Contributor

mathieudutour commented Sep 23, 2018

Even if it’s not an action there is a context

@nicutimofte

This comment has been minimized.

Copy link
Author

nicutimofte commented Sep 24, 2018

You're right. Now I'm using as you suggested const document = sketch.Document.fromNative(context.document), but unexpectedly it creates and opens a new sketch document(named "Untitled") and document contains the data of the newly created document.

@lucaorio

This comment has been minimized.

Copy link

lucaorio commented Feb 6, 2019

@mathieudutour I see a merged PR, but I'm still experiencing this. Perhaps I'm doing something wrong?

import { Document, Shape, UI, Style, Page } from "sketch";

const generate = () => {
  const document = Document.getSelectedDocument();
};

export default generate;

console> undefined

@mathieudutour

This comment has been minimized.

Copy link
Contributor

mathieudutour commented Feb 6, 2019

Nothing wrong but it's not published yet, maybe in 53.1 or .2

@lucaorio

This comment has been minimized.

Copy link

lucaorio commented Feb 6, 2019

That's great news, thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment