Last active
January 8, 2018 10:55
-
-
Save melvitax/fd592a162ad4fe48bd57 to your computer and use it in GitHub Desktop.
AppleScript for organizing Android assets. To turn into a Service: In Automator, create a new Service for files in Finder and add this script. From the Finder select a group of files that have densitiy suffixes like "icon-hdpi.png" and "icon-mdpi.png" and run the script. The files will be organized into the appropiate drawable folders and renamed.
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
property sizeList : {"mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"} | |
property extensionList : {"jpg", "jpeg", "png"} | |
on run {input, parameters} | |
repeat with i from 1 to the count of input | |
set thisItem to item i of input | |
set itemInfo to info for thisItem | |
set fileName to name of itemInfo | |
set fileExt to the name extension of itemInfo | |
tell application "Finder" | |
set parentFolder to container of thisItem as Unicode text | |
end tell | |
if fileExt is in extensionList then | |
repeat with idx from 1 to the count of sizeList | |
set thisSize to item idx of sizeList | |
if fileName contains thisSize then | |
set targetFolder to createFolderIfNeeded(thisItem, thisSize, parentFolder) | |
set findString to "-" & thisSize | |
set newName to replaceText(findString, "", fileName) | |
tell application "Finder" | |
-- Replace file if it exists | |
set newFilePath to targetFolder & ":" &newName | |
if exists (newFilePath) then | |
delete file newFilePath | |
end if | |
-- Move file and rename | |
set fileMoved to move thisItem to targetFolder | |
set theFileToRename to fileMoved as alias | |
set name of theFileToRename to newName | |
end tell | |
end if | |
end repeat | |
end if | |
end repeat | |
return input | |
end run | |
on createFolderIfNeeded(thisItem, thisSize, parentFolder) | |
tell application "Finder" | |
set folderName to "drawable-" & thisSize | |
set folderPath to parentFolder & folderName | |
if not (exists (folderPath)) then | |
make new folder at parentFolder with properties {name:folderName} | |
end if | |
end tell | |
return folderPath | |
end createFolderIfNeeded | |
on replaceText(find, replace, subject) | |
set prevTIDs to text item delimiters of AppleScript | |
set text item delimiters of AppleScript to find | |
set subject to text items of subject | |
set text item delimiters of AppleScript to replace | |
set subject to "" & subject | |
set text item delimiters of AppleScript to prevTIDs | |
return subject | |
end replaceText |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment