Skip to content

Instantly share code, notes, and snippets.

@fatihturan
Created March 11, 2015 13:50
Show Gist options
  • Save fatihturan/62a30430fffcdee98722 to your computer and use it in GitHub Desktop.
Save fatihturan/62a30430fffcdee98722 to your computer and use it in GitHub Desktop.
Android Slice Folder Organizer Service for OS X
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