Skip to content

Instantly share code, notes, and snippets.

@mac-cain13
Last active August 29, 2015 14:11
Show Gist options
  • Save mac-cain13/91d4641240ea9cb3394d to your computer and use it in GitHub Desktop.
Save mac-cain13/91d4641240ea9cb3394d to your computer and use it in GitHub Desktop.
Automatically copy all PNG assets to the correct Android drawable folder when exporting from for example Sketch.

Android assets folder action

Automatically copy all PNG assets to the correct Android drawable folder. Very helpful when exporting assets with @?x-suffix from, for example, Sketch and putting them into the correct folder without human interaction.

Setup

  1. Copy the AppleScript file to ~/Library/Workflows/Applications/Folder Actions
  2. Create a export folder where you'll put all exported PNGs
  3. Right click the folder, choose configure folder actions, check the "Enable folder actions" checkbox and attach the android_assets.scpt to your folder
  4. Open the terminal and create a symbolic link by typing ln -s /path/to/android-project/app/src/main/res /path/to/export-folder-you-just-created

Now just export PNGs into this folder with the suffix @1x through @4x and the script will copy them into the correct folder. For example icn_towel@1,5x.png will be copied to drawable-hdpi/icn_towel.png.

-- Author: Mathijs Kadijk
-- License: MIT License
-- From: https://gist.github.com/mac-cain13/91d4641240ea9cb3394d
on adding folder items to this_folder after receiving added_items
set resolutions to {{suffix:"@4x", folder:"xxxhdpi"}, {suffix:"@3x", folder:"xxhdpi"}, {suffix:"@2x", folder:"xhdpi"}, {suffix:"@1,5x", folder:"hdpi"}, {suffix:"@1.5x", folder:"hdpi"}, {suffix:"@1x", folder:"mdpi"}}
repeat with the_item in the added_items
try
tell application "Finder"
set the item_extension to the name extension of the_item
set the item_name to the name of the_item
set this_folder_path to POSIX path of this_folder
end tell
repeat with resolution in the resolutions
set ending to suffix of resolution & "." & item_extension
if item_name ends with ending then
set destination_folder_path to this_folder_path & "res/drawable-" & folder of resolution
set destination_filename to trim_line(item_name, ending, 1) & "." & item_extension
set destination_path to destination_folder_path & "/" & destination_filename
do shell script "mv " & quoted form of POSIX path of the_item & " " & quoted form of destination_path
end if
end repeat
end try
end repeat
end adding folder items to
----
on trim_line(this_text, trim_chars, trim_indicator)
-- 0 = beginning, 1 = end, 2 = both
set x to the length of the trim_chars
-- TRIM BEGINNING
if the trim_indicator is in {0, 2} then
repeat while this_text begins with the trim_chars
try
set this_text to characters (x + 1) thru -1 of this_text as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
-- TRIM ENDING
if the trim_indicator is in {1, 2} then
repeat while this_text ends with the trim_chars
try
set this_text to characters 1 thru -(x + 1) of this_text as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
return this_text
end trim_line
@tomasharkema
Copy link

Hey @mac-cain13 !!

Could you look into exporting @1x assets without the actual suffix? Sketch isn't doing that by default, so it could save me lots of time :D

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