Skip to content

Instantly share code, notes, and snippets.

@zmwangx
Created January 2, 2015 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmwangx/d6406fb8bf51ac768770 to your computer and use it in GitHub Desktop.
Save zmwangx/d6406fb8bf51ac768770 to your computer and use it in GitHub Desktop.
Dropzone 3 custom action: attach "__" and the first seven digits of the SHA-1 digest of a file to its filename (intended to resolve naming conflicts and facilitate sharing over the Internet).
# Dropzone Action Info
# Name: 7sha1
# Description: Attach "__" and the first seven digits of the SHA-1 digest of a file to its filename (intended to resolve naming conflicts and facilitate sharing over the Internet).
# Handles: Files
# Creator: Zhiming Wang
# URL: https://github.com/zmwangx
# Events: Clicked, Dragged
# KeyModifiers: Command, Option, Control, Shift
# SkipConfig: No
# RunsSandboxed: No
# Version: 1.0
# MinDropzoneVersion: 3.0
require "digest"
require "fileutils"
def rename(items)
num_items = items.length
# if no items, do nothing
if num_items == 0
$dz.url(false)
return
end
# process each item
$dz.determinate(true)
num_done = 0
items.each {|path|
percentage = (100.0 / num_items * num_done).round
$dz.begin(File.basename(path))
$dz.percent(percentage)
if ! File.directory?(path)
extname = File.extname(path)
basename = File.basename(path, extname)
dirname = File.dirname(path)
new_basename = basename + "__" + Digest::SHA1.file(path).hexdigest[0,7]
new_path = File.join(dirname, new_basename + extname)
FileUtils.mv(path, new_path)
end
num_done += 1
}
$dz.finish("Jobs complete")
$dz.url(false)
end
def dragged
rename($items)
end
def clicked
items_string = `osascript <<END
set pathList to ""
tell application "Finder"
set theSelection to the selection
if theSelection = {} then return
repeat with theItem in theSelection
set pathList to pathList & POSIX path of (theItem as text) & linefeed
end repeat
end tell
return text 1 through -2 of pathList
END`
items = items_string.split("\n")
rename(items)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment