Coder Social home page Coder Social logo

aednlaxer / python_sketch_api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zahlii/python_sketch_api

0.0 1.0 0.0 22.23 MB

Python API to access and modify files from the "Sketch" software

License: GNU General Public License v3.0

Python 100.00%

python_sketch_api's Introduction

python-sketch-api

Python API to access and modify files from the "Sketch" software (https://www.sketchapp.com/), based on a Python object structure with typing hints. Allows reading binary "plists", and consequently allows you to query information about text layers!

Includes all relevant data, even metadata and saving information.

Examples

Writing

main_file = SketchFile.from_file('Icons.sketch')

symbol_hello = main_file.search_symbols_by_name('HALLO')[0]
symbol_comp = main_file.search_symbols_by_name('Comp')[0]
symbol_add = main_file.search_symbols_by_name('Add')[0]

for s in [symbol_hello, symbol_comp, symbol_add]:
    print(s.name, s.do_objectID, s.symbolID, s.originalObjectID)

target_page = main_file.sketch_pages[1]

if main_file.has_page('Test2'):
    main_file.remove_page('Test2')

test_page = main_file.add_page('Test2')

test_artboard = sketch_types.SJArtboardLayer.create('Artboard 123424525245', 500, 500)
test_page.add_artboard(test_artboard)

rect = sketch_types.SJShapeRectangleLayer.create('Rect ABC', 10, 10, 100, 100)
test_artboard.add_layer(rect)

l = sketch_types.SJSymbolInstanceLayer.create(symbol_hello, 50, 50)
l.add_symbol_override(symbol_hello.get_layer_by_type('symbolInstance')[0].do_objectID, symbol_add)
l.add_text_override(symbol_hello.get_layer_by_type('text')[0].do_objectID, 'FUCKYEAH')

l3 = sketch_types.SJSymbolInstanceLayer.create(symbol_hello, 80, 80)
l3.add_symbol_override(symbol_hello.get_layer_by_type('symbolInstance')[0].do_objectID, symbol_add)
l3.add_text_override(symbol_hello.get_layer_by_type('text')[0].do_objectID, 'FUCKYEAH2')

l_group = sketch_types.SJGroupLayer.create('Group Me', [l, l3])

test_artboard.add_layer(l_group)

pts = [sketch_types.Point(300, 200), sketch_types.Point(500,200), sketch_types.Point(50,23)]
l_path = sketch_types.SJShapePathLayer.create('Test Path', pts)

test_artboard.add_layer(l_path)

# source_str = sketch_io.PyToSketch.write(test_page)

print()

main_file.save_to('created.sketch')

Reading

file = SketchFile.from_file('MyFile.sketch')


file.sketch_pages

[<sketch_types.SketchPage at 0x10d64f908>,
 <sketch_types.SketchPage at 0x10d64fb70>,
 <sketch_types.SketchPage at 0x10d658e80>,
 <sketch_types.SketchPage at 0x10d656128>,
 <sketch_types.SketchPage at 0x10f03a4e0>,
 <sketch_types.SketchPage at 0x10f09f470>,
 <sketch_types.SketchPage at 0x10f0b07b8>,
 <sketch_types.SketchPage at 0x10d6560f0>,
 <sketch_types.SketchPage at 0x10f167da0>,
 <sketch_types.SketchPage at 0x10f239ba8>,
 <sketch_types.SketchPage at 0x10f31eeb8>,
 <sketch_types.SketchPage at 0x10f525748>,
 <sketch_types.SketchPage at 0x10f52a278>]

file.sketch_pages[0].layers[1]
Out[4]: <sketch_types.SJTextLayer at 0x10d64fdd8>

file.sketch_pages[0].layers[1].attributedString.get_font_family()
Out[5]: 'OpenSans-Bold'

file.sketch_document.userInfo
Out[7]: {'com.invisionlabs.sync': {'metadata': '{"hasSynced":0,"hasProjectIdChanged":0,"projectId":10489170,"publicLink":"","resolution":2,"syncAll":false}'}}

file.sketch_document.assets.colors
Out[8]: 
[<sketch_types.SJColor at 0x10d6bc2e8>,
 <sketch_types.SJColor at 0x10d6bc358>,
 <sketch_types.SJColor at 0x10d6bc390>,
 <sketch_types.SJColor at 0x10d6bc4e0>]

file.sketch_meta.pagesAndArtboards[file.sketch_pages[0].do_objectID].name
Out[13]: '- Introduction v0.3.0'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.