Last active
August 29, 2015 14:22
-
-
Save bob-codingdutchmen/1f166e34e98da1edea5f to your computer and use it in GitHub Desktop.
Export Sketch file to android dpi-folders without slices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import argparse | |
from subprocess import call | |
""" | |
This script should run with any recent version of python (2.7 or 3+) | |
For this to run, sketchtool should be available (download from http://bohemiancoding.com/sketch/tool/ ) | |
To use this from anywhere, put the file in a folder on your $PATH (`/usr/local/bin` for example), | |
Give it execute permission by running: | |
`chmod +x sketchandroid` | |
Then run from anywhere: | |
`sketchandroid Assets.sketch -e export/android` | |
""" | |
def sketch_export(file, export_dir='export'): | |
format = 'sketchtool export artboards {file} --output="{export}/drawable-{dpi}" --scales="{scale}"' | |
commands = [ | |
format.format(file=file, dpi='mdpi', scale='1.0', export=export_dir), | |
format.format(file=file, dpi='hdpi', scale='1.5', export=export_dir), | |
format.format(file=file, dpi='xhdpi', scale='2.0', export=export_dir), | |
format.format(file=file, dpi='xxhdpi', scale='3.0', export=export_dir) | |
] | |
for command in commands: | |
call(command, shell=True) | |
def rename_dots(folder): | |
rootdir = os.path.join(os.path.dirname(__file__), folder) | |
count = 0 | |
for root, subFolders, files in os.walk(rootdir): | |
pngs = [f for f in files if '.png' in f] | |
for png in pngs: | |
name, ext = os.path.splitext(png) | |
if '.' in name.rstrip('.9'): | |
name = name.replace('.', '_') | |
if name.endswith('_9'): | |
name = name[:-2] + '.9' | |
os.rename( | |
os.path.join(root, png), | |
os.path.join(root, name + ext) | |
) | |
count += 1 | |
print('Renamed {} files to remove periods'.format(count)) | |
def rename_scale_indication(folder): | |
rootdir = os.path.join(os.path.dirname(__file__), folder) | |
count = 0 | |
for root, subFolders, files in os.walk(rootdir): | |
pngs = [f for f in files if '@1x.png' in f] | |
pngs += [f for f in files if '@2x.png' in f] | |
pngs += [f for f in files if '@3x.png' in f] | |
for png in pngs: | |
name, ext = os.path.splitext(png) | |
newname = png.replace('@1x', '') | |
newname = newname.replace('@2x', '') | |
newname = newname.replace('@3x', '') | |
os.rename(os.path.join(root, png), os.path.join(root, newname)) | |
count += 1 | |
print('Renamed {} files to remove @1x, @2x, @3x'.format(count)) | |
parser = argparse.ArgumentParser(description='Sketch to Android assets') | |
parser.add_argument( | |
'filename', | |
help='Path to sketch file' | |
) | |
parser.add_argument( | |
'-e', | |
action='store', | |
dest='export_folder', | |
help='folder to export the assets to.' | |
) | |
args = parser.parse_args() | |
folder_arg = args.export_folder | |
if not os.path.isabs(folder_arg): | |
folder = os.path.join(os.getcwd(), args.export_folder or 'export') | |
sketch_export(args.filename, export_dir=folder) | |
rename_scale_indication(folder) | |
rename_dots(folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could use some optimization, but this works for now