Skip to content

Instantly share code, notes, and snippets.

@shirishgone
Created December 19, 2013 08:58
Show Gist options
  • Save shirishgone/8036382 to your computer and use it in GitHub Desktop.
Save shirishgone/8036382 to your computer and use it in GitHub Desktop.
Ruby Script to create AssetCatalogs file in iOS Application. It basically fetches all the .png files from the application folder and subfolders and create a single AssetCatalog files inside the application. Most of the design developer collaboration issues will be sorted out with this script. Ask the designer to give all the assets and put into …
require 'find'
require 'fileutils'
require 'tmpdir'
def createAssetCatalogFile()
message = '
Creating Asset Catalogs file ...'
puts message
fileName = "images.xcassets"
projectFolderName = Dir.getwd
# Create asset catalog file if doesnt exists
if File.directory?("#{fileName}") == false
Dir.mkdir(fileName)
end
# Fetch all the png files from the project folder
Dir.glob("#{projectFolderName}/**/*.png").each do |asset|
#Jump into asset catalog folder
Dir.chdir(fileName)
assetName = File.basename(asset,'.*')
assetNameWithout2x = ""
if assetName.include? "@2x"
assetNameWithout2x = assetName.gsub("@2x", "")
imageSetName = "#{ assetNameWithout2x }.imageset"
else
imageSetName = "#{ assetName }.imageset"
end
#If directory doesnt exists, create new ImageSet directory which holds images
if File.directory?("#{imageSetName}") == false
Dir.mkdir(imageSetName)
end
#Jump into ImageSet directory
Dir.chdir(imageSetName)
FileUtils.mv("#{asset}", "#{assetName}.png")
#Create Json File which needs to be present for every ImageSet
createJsonfile(assetNameWithout2x)
#Jump back to parent directory
Dir.chdir(projectFolderName)
end
end
def createJsonfile(fileName)
fileName_1x = "#{fileName}.png"
fileName_2x = "#{fileName}@2x.png"
content = '{
"images" : [
{
"idiom" : "universal",
"scale" : "1x",
"filename" : "'"#{ fileName_1x }"'"
},
{
"idiom" : "universal",
"scale" : "2x",
"filename" : "'"#{ fileName_2x }"'"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}'
target = "Contents.json"
File.open(target, "w+") do |f|
f.write(content)
end
end
createAssetCatalogFile()
@hshah2811
Copy link

Do you have updated script ? Or it will work with Xcode 6?

@mmislam101
Copy link

Thanks for the script! You should update it for XCode 6 and gear it towards helping people generate assets for their animations for the Apple Watch.

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