Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

« Newer Snippets
Older Snippets »
7 total  XML / RSS feed 

Ruby: Apple Aperture API

UPDATED WED Aug 15/07

PLEASE NOTE: requires the following modules "active_record", "rbosa" and "hpricot" and my own Imageshack API to work.

USAGE:

You can include/require this as a module in your own scripts, or as I do, use it directly from irb in the Terminal. Documentation is provided within the script itself, but essentially I use it to get these things done:

With Aperture launched, select the pictures of your choice (one or multiples makes no diff), then entering the following inside irb results in:

1. Aperture.reveal is essentially "Reveal in Finder"

2. Aperture.paths returns the Unix paths to the locations of the pics.

3. Aperture.open opens the pictures with an external default app (usually Preview).

4. If you provide the script with Photoshop's path, then:

Aperture.edit will open the original file directly in Photoshop without creating a separate version inside Aperture.

5. Aperture.ids will give you an array of the unique ID asssigned by Aperture to each pic within the ZRKVERSION table. What this is for is up to you to find out.

6. NEW: Imageshack.us upload/backup

Aperture.upload will upload the pic(s) to Imageshack.us and return the direct link/URL of the pic.

Upon first launch, a new metadata field is created inside Aperture's metadata panel called "Imageshack URL" (if you don't see it, inside Aperture, Ctrl-D --> Others). the uploaded pic's URL will be stored inside this field automatically.

The API directly interrogates Aperture's SQLite DB. Who knows, could be a nice place to start from to building other stuff.



require 'rubygems'
require 'active_record'
require 'rbosa'
require 'hpricot'
require 'open3'
require 'beckett/imageshack'

# --- GLOBALS --- #

HOME = ENV['HOME']
PATH_TO_PHOTOSHOP = "/Applications/Adobe\\ Photoshop\\ CS3/Adobe\\ Photoshop\\ CS3.app/Contents/MacOS/Adobe\\ Photoshop\\ CS3"

Aperture = OSA.app('Aperture')

# --- CONNECT TO CURRENT LIBRARY --- #

class LibraryConnect

  def initialize
    findLibrary
    connect unless $PATH_TO_DB.nil?
  end

  protected

  def findLibrary
  
  aperture_plist = HOME + '/Library/Preferences/com.apple.Aperture.plist'
  plistdump = HOME + '/Library/Caches/' + 'plistdump.tmp'
  plutil_error = Open3.popen3("plutil -convert xml1 -o #{plistdump} #{aperture_plist}") { |stdin,stdout,stderr| stderr.read }

  if plutil_error == ""
      doc = Hpricot(File.open(plistdump)) ; %x( rm #{plistdump} )
      relative_lib_path = (doc/:key).select { |key| key.inner_text == 'LibraryPath' }[0].next_sibling.inner_text
      $PATH_TO_LIB = relative_lib_path =~ /^~/ ? relative_lib_path.gsub(/^~/, HOME) : relative_lib_path
      $PATH_TO_DB = "#{$PATH_TO_LIB}/Aperture.aplib/Library.apdb"
      puts "Current library --> " + $PATH_TO_LIB
  else
      $PATH_TO_DB = nil
      puts "Cannot find library. Goodbye."
  end

  end

  def connect
    ActiveRecord::Base.establish_connection(
      :adapter => "sqlite3",
      :dbfile  => $PATH_TO_DB
    )
  end

end

# --- LIBRARY CLASSES-SQL TABLES MAPPINGS --- #

class Pictures < ActiveRecord::Base         # Contains the majority of non-metadata info on a picture
  set_table_name "ZRKVERSION"
  set_primary_key "ZUUID"
end

class Projects < ActiveRecord::Base         # Every pic only belongs to one project
  set_table_name "ZRKFOLDER"
  set_primary_key "ZUUID"
end

class Masters < ActiveRecord::Base          # Info on master images
  set_table_name "ZRKMASTER"
  set_primary_key "Z_PK"
end

class Files < ActiveRecord::Base            # Info on the actual image file 
  set_table_name "ZRKFILE"
  set_primary_key "Z_PK"
end

class Metadata < ActiveRecord::Base         # Metadata information tagged to every picture in library
  set_table_name "ZRKSEARCHABLEPROPERTY"
  set_primary_key "Z_PK"
end

class Albums < ActiveRecord::Base           # All albums within library
  set_table_name "ZRKPERSISTENTALBUM"
  set_primary_key "Z_PK"
end

class AlbumVersions < ActiveRecord::Base    # Foreign key that links a picture's Z_PK (from ZRKVERSION) to an
  set_table_name "Z_11VERSIONS"             # album's Z_PK in ZRKPERSISTENTALBUM
end

class Properties < ActiveRecord::Base       # Metadata categories
  set_table_name "ZRKPROPERTYIDENTIFIER"
  set_primary_key "Z_PK"
end

class Picture                               # Custom class that contains all relevant info on a picture

attr_reader :id, :project_id, :master_id, :file_id, :version_id, :width, :height, :filesize, :name, :moddate

def initialize(pic_id)

  # Query ZRKVERSION
  
  @id = pic_id
  pic = Pictures.find(pic_id)
  @project_id = pic.ZPROJECTUUID
  @master_id = pic.ZMASTER
  @file_id = pic.ZFILE
  @version_id = pic.Z_PK

  # Query ZRKFILE

  file = Files.find(@file_id)
  @width = pic.ZPIXELWIDTH
  @height = pic.ZPIXELHEIGHT
  @filesize = file.ZFILESIZE
  @name = file.ZNAME
  @moddate = realtime(file.ZFILEMODIFICATIONDATE_before_type_cast)

end

def filepath
    path = $PATH_TO_LIB + '/' + self.projectPath + '/' + self.importGroup + '/' + self.folderName + '/' + self.filename
end

def nixpath
    self.filepath.gsub(/ /,'\ ')
end

def open
  `open #{self.nixpath}`
end

def edit
  if PATH_TO_PHOTOSHOP != ""
    `open -a #{$PATH_TO_PHOTOSHOP} #{self.nixpath}`
  else
    `open #{self.nixpath}`
  end
end

def reveal
    puts nixpath
    `open #{File.dirname(self.nixpath)}`
end

def shack_uri
  pic_meta = Metadata.find( :first, :conditions => { :ZVERSION => @version_id, :ZPROPERTYIDENTIFIER => $SHACKURLPROP } )
  unless pic_meta == nil
    pic_meta.ZPROPERTYSPECIFICSTRING
  else
    return 'Does not exist.'
  end
end

def storeURI(url)
records = Metadata.find( :all, :conditions => { :ZVERSION => @version_id, :ZPROPERTYIDENTIFIER => $SHACKURLPROP } )
if  records.empty? == true
    new_uri = Metadata.create(
        :Z_ENT => 13,
        :Z_OPT => 1,
        :ZPROPERTYSPECIFICNUMBER =>  nil,
        :ZPROPERTYSPECIFICSTRING => url,
        :ZVALUETYPE => 3,
        :ZVERSION => @version_id,
        :ZPROPERTYIDENTIFIER => $SHACKURLPROP )
  new_uri.Z_PK
else
  records.each { |entry| Metadata.destroy(entry.Z_PK) }
  self.storeURI(url)
end

end

def realtime(sqtime)
  t = Time.at(sqtime.to_i + 978307200)      # => # Derives from: Time.utc(2001,"jan",1,0,0,0).to_i
  Time.utc(t.year,t.month,t.day,t.hour,t.min)
end

protected

def projectPath
    Projects.find(@project_id).ZLIBRARYRELATIVEPATH
end

def importGroup
   Masters.find(@master_id).ZIMPORTGROUP
end

def folderName
   Masters.find(@master_id).ZNAME
end

def filename
  Files.find(@file_id).ZNAME
end

end

class Album

def initialize(zpk)
  @album = Albums.find(zpk)
end

def add_count(n=0)
  @album.update_attributes(   :ZDATELASTSAVEDINDATABASE => (Time.now.to_f + 978307200.0),
                              :ZMETADATACHANGEDATE => (Time.now.to_f + 978307200.0),
                              :ZVERSIONCOUNT => @album.ZVERSIONCOUNT + n,
                              :ZCREATEDATE => @album.ZCREATEDATE_before_type_cast.to_f )
end

end

# --- INTERACTING WITH APERTURE FROM IRB --- #
# All operations are performed recursively on image(s) within the array returned by Aperture

class OSA::Aperture::Application

def new_prop(fieldName)

Properties.create(  :Z_ENT => 12,
                    :Z_OPT => 1,
                    :ZPROPERTYKEY => fieldName,
                    :ZPROPERTYTYPE => 7)
                    
return Properties.find_by_ZPROPERTYKEY(fieldName).Z_PK

end

def ids     # Returns the IDs used to query the ZRKVERSION table
  self.selection.collect { |pic| pic.id2 }
end

def paths   # Returns the filepaths of selected images
  self.ids.collect { |id| Picture.new(id).filepath }
end

def reveal  # 'Reveal in Finder' selected images
  self.ids.each { |id| Picture.new(id).reveal }
end

def open    # Opens selected images in Preview
  self.ids.each { |id| Picture.new(id).open }
end

def edit    # Opens the original file in Photoshop CS3 (if path provided or default)
  self.ids.each { |id| Picture.new(id).edit }
end

def upload(reveal=false)  # Uploads the image to Imageshack and returns the direct link to the pic, if called with true, then opens it in browser after upload

  self.ids.each do |id|
    pic = Picture.new(id)
    pic_mirror = ShackMirror.new(pic.filepath)
    pic.storeURI(pic_mirror.url)
    `open #{pic_mirror.url}` if reveal
    puts pic_mirror.url
  end

end

def print(name)
  self.ids.each { |id| puts Picture.new(id).send(name) }
end

end

# --- INITIALIZE --- #

LibraryConnect.new

$SHACKURLPROP = Properties.find_by_ZPROPERTYKEY("Imageshack URL") == nil ? Aperture.new_prop("Imageshack URL") : Properties.find_by_ZPROPERTYKEY("Imageshack URL").Z_PK

Ruby: Apple Shake - Report FileIn Nodes

Another quick and dirty script I wrote when I screwed up my media's filesystem and I had to put it back together and this script simply prints out (nicely) the location of the files of every FileIn node in the Shake script.


#! /usr/bin/env ruby

$SOURCE = "" # Where are the Shake scripts?

$FILE_LIST = %x| ls #{$SOURCE}*.shk |.split("\n")

$FILE_LIST.each do |f|
  
  filein_nodes = File.open(f).grep(/SFileIn/)

  puts " --------- #{f} ------------"

  filein_nodes.map! { |n| n.split(/SFileIn/).last }

  filein_nodes.each { |n| puts n }
  
  puts "\n\n"

end

Ruby: Statistical Arrays


require 'arrayx' # separate post

# Statistical methods for arrays. Also see NArray Ruby library.

class Float

  def roundf(decimel_places)
      temp = self.to_s.length
      sprintf("%#{temp}.#{decimel_places}f",self).to_f
  end

end

class Integer

  # For easy reading e.g. 10000 -> 10,000 or 1000000 -> 100,000
  # Call with argument to specify delimiter.

  def ts(delimiter=',')
    st = self.to_s.reverse
    r = ""
    max = if st[-1].chr == '-'
      st.size - 1
    else
      st.size
    end
    if st.to_i == st.to_f
      1.upto(st.size) {|i| r << st[i-1].chr ; r << delimiter if i%3 == 0 and i < max}
    else
      start = nil
      1.upto(st.size) {|i|
        r << st[i-1].chr
        start = 0 if r[-1].chr == '.' and not start
        if start
          r << delimiter if start % 3 == 0 and start != 0  and i < max
          start += 1
        end
      }
    end
    r.reverse
  end

end

class Array

  def sum
    inject( nil ) { |sum,x| sum ? sum+x : x }
  end

  def mean
    sum=0
    self.each {|v| sum += v}
    sum/self.size.to_f
  end

  def variance
    m = self.mean
    sum = 0.0
    self.each {|v| sum += (v-m)**2 }
    sum/self.size
  end

  def stdev
    Math.sqrt(self.variance)
  end

  def count                                 # => Returns a hash of objects and their frequencies within array.
    k=Hash.new(0)
    self.each {|x| k[x]+=1 }
    k
  end
    
  def ^(other)                              # => Given two arrays a and b, a^b returns a new array of objects *not* found in the union of both.
    (self | other) - (self & other)
  end

  def freq(x)                               # => Returns the frequency of x within array.
    h = self.count
    h(x)
  end

  def maxcount                              # => Returns highest count of any object within array.
    h = self.count
    x = h.values.max
  end

  def mincount                              # => Returns lowest count of any object within array.
    h = self.count
    x = h.values.min
  end

  def outliers(x)                           # => Returns a new array of object(s) with x highest count(s) within array.
    h = self.count                                                              
    min = self.count.values.uniq.sort.reverse.first(x).min
    h.delete_if { |x,y| y < min }.keys.sort
  end

  def zscore(value)                         # => Standard deviations of value from mean of dataset.
    (value - mean) / stdev
  end

end

Ruby: Extended Arrays & Hashes (arrayx)


require 'set'

class Array

# Performs delete_if and returns the elements deleted instead (delete_if will return the array with elements removed).
# Because it is essentially delete_if, this method is destructive. Also this method is kinda redundant as you can simply
# call dup.delete_if passing it the negation of the condition.

def delete_fi
  x = select { |v| v if yield(v) }
  delete_if { |v| v if yield(v) }
  x.empty? ? nil : x
end

# Turns array into a queue. Shifts proceeding n elements forward
# and moves the first n elements to the back. When called with
# a block, performs next! and returns the result of block
# performed on that new first element. This method is destructive.

# a = [1,2,3].next!  => [2,3,1]
# a.next! { |x| x+ 10 } => 13
# a is now [3,1,2]

def next!(n=1)
  n.times do
    push(shift)
  end
  if block_given?
    y = yield(first)
    y
  else
    self
  end
end

# Treats [x,y] as a range and expands it to an array with y elements
# from x to y.

# [1,10].expand => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# [1,10].expand { |x| x**2 } => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

def expand
  x = (first..last).to_a
  if block_given?
    x.collect { |x| yield(x) }
  else
    x
  end
end

def shuffle!
   each_index do |i|
     j = rand(length-i) + i
     self[j], self[i] = self[i], self[j]
   end
end

def pick(n=1)
  y = Set.new
  until y.size == n
    t = self[rand(size)]
    y.add(t)
  end
  y.to_a
  y.to_a.first if n == 1
end

# ======== MATRIX CRUD ========= #

# Turns [x,y] into a matrix with x rows and y columns and fills them
# with the result of block. Block must be given *without* arguments.
# If no block is given, the method yields a sparse matrix.

# m = [3,3].to_matrix { rand(30) }
# => [[20, 26, 5, 14, 10], [20, 0, 28, 21, 18], [21, 16, 20, 12, 11]]

def to_matrix
  row = first
  col = last
  if block_given?
    x = Array.new(row) { Array.new(col) { yield } }
  else
    x = Array.new(row) { Array.new(col,0) }
  end
  x
end

def each_coordinate
  each_with_index do |row,x|
    row.each_with_index do |col,y|
      yield(x,y)
    end
  end
end

def mx_lookup(row,col)
  if row < 0 || col < 0
    nil
  else
    self[row][col]
  end
end

def mx_assign(row,col,val)
  self[row][col] = val
end

def mx_update(row,col,new_val)
  self[row][col] = new_val
end

end

class Hash

# Performs delete_if and returns the elements deleted. Because
# it is essentially delete_if, this method is destructive.

def delete_fi
  x = select { |k,v| yield(k,v) }
  delete_if { |k,v| yield(k,v) }
  x.empty? ? nil : x
end

def collect
  x = select { |k,v| yield(k,v) }
  h = x.inject({}) { |h,v| h.update x.first => x.last }
  h
end

end

class Range
  
# Returns an array with n random numbers within range.
  
        def pick(n=1)
    y = []
          x = [first,last].expand
          n.times { y << x[rand(x.size)] }
    y
    y.first if n == 1
        end
        
end

Ruby: Barebones Ruby Imageshack API

Usage:

pic_online = ShackMirror.new(local_path_of_pic)
pic_online.url # => returns direct link on Imageshack.

NOTE: if you give it a path beginning with http, then it will transload the image directly to Imageshack. Cool!

require 'rubygems'
require 'hpricot'
require 'net/http'
require 'uri'
require 'cgi'
require 'mime/types'

class ShackMirror

SHACK_ID = "REPLACE WITH YOUR OWN IMAGESHACK ID"
USER_AGENT = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/419 (KHTML, like Gecko) Safari/419.3"
BOUNDARY = '----------PuSHerInDaBUSH_$'

attr_reader :url

def initialize(img)
    raise NonImageTypeError, 'Expected image file.' unless img =~ /jpe?g|png|gif|bmp|tif|tiff|swf$/
    @img = img
    @url, @hosturi, @res = "","",""
    @header, @params = {}, {}
    @header['Cookie'] = "myimages=#{SHACK_ID}"
    @header['User-Agent'] = USER_AGENT
    @params['uploadtype'] = 'on'  
    @params['brand'] = ''
    @params['refer'] = ''
    @params['MAX_FILE_SIZE'] = '13145728'
    @params['optimage'] = '0'
    @params['rembar'] = '1'
    transfer
    getdirect
end

protected

def prepare_multipart ( params )
    fp = []
    params.each do |k,v|
    if v.respond_to?(:read)
      fp.push(FileParam.new(k,v.path,v.read))
      else fp.push(Param.new(k,v)) 
    end
  end
    query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
    return query
end

def prepFile(path_to_file)

  file = File.new(path_to_file)

  @header['Content-Type'] = "multipart/form-data, boundary=" + BOUNDARY + " "

  @params['url'] = 'paste image url here'
  @params['fileupload'] = file
  
  $query = prepare_multipart(@params)
  file.close

end

def locate(path)
  path !~ /^http/ ? "local" : "remote"
end

def upload( query, headers={} )
  Net::HTTP.start(@hosturi.host) do | http |
    http.post(@hosturi.path, query, headers);
  end
end

def transload(url)

  @header['Content-Type'] = 'form-data'

  @params['url'] = url
  @params['fileupload'] = ''
  
  postreq = Net::HTTP::Post.new(@hosturi.path, @header)
  postreq.set_form_data(@params)

  return Net::HTTP.new(@hosturi.host, @hosturi.port).start { |http| http.request(postreq) }

end

def transfer

case locate(@img)
  when "local"
    @hosturi = URI.parse('http://load.imageshack.us/index.php')
    prepFile(@img)
    @res = upload($query,@header)
  when "remote"
    @hosturi = URI.parse('http://imageshack.us/transload.php')
    @res = transload(@img)
end

end

def getdirect
  doc = Hpricot(@res.body)
  @url = (doc/"//input").last['value']
end

end

class Param

  attr_accessor :k, :v

  def initialize( k, v )
    @k = k
    @v = v
  end
  
  def to_multipart
    return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
  end

end

class FileParam

  attr_accessor :k, :filename, :content

  def initialize( k, filename, content )
    @k = k
    @filename = filename
    @content = content
  end
  
  def to_multipart
    return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" +
    "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n"
  end

end

Ruby: Marshal/Serialize Objects the Easy Way

// description of your code here

Serializes an object by simply calling marshal(name,object). When called without the second argument, it deserializes and recalls the object instead.


# save as marshalize.rb in /usr/local/lib/ruby/1.8/ and then require 'marshalize'

TMP_DIR = "/Library/Caches/"

def marshal(filename,data=nil)
  Dir.chdir(TMP_DIR) do
    if data != nil
      open(filename, "w") { |f| Marshal.dump(data, f) }
    elsif File.exists?(filename)
      open(filename) { |f| Marshal.load(f) }
    end
  end
end

def marshal_destroy(filename)
  Dir.chdir(TMP_DIR) do
  if File.exists?(filename)
    File.delete(filename)
  else
    return "File does not exists."
  end
  end
end

def marshal_clone(data)
  filename = srand.to_s << '.tmp'
  marshal(filename,data)
  h = marshal(filename)
  marshal_destroy(filename)
  return h
end

Ruby: Submit Apple Shake Scripts for Batch Render

A quick-n-dirty Ruby script that takes all the Shake scripts in a given folder, checks to make sure they are all set on maximum quality e.g "Base" and then submits them for render.

#! /usr/bin/env ruby

require 'pp'

class Fixnum

def pad(n)
  
  x = self.to_s

  if x.length < n
    pad = "0"*(n - x.length)
    x.insert(0,pad)
  else
    x
  end

end

end

def render(filepath,last_frame)

IO.popen("shake -v xml -exec #{filepath} -t 1-#{last_frame}x1", 'r+')

end

def get_last_frame(file_path)
  
  last_frame = File.open(file_path).grep(/SetTimeRange/).to_s.scan(/\d{2,6}/).first.to_i

end

def get_output_file(file_path)

  out_file = File.open(file_path).grep(/FileOut/).first.gsub(/\"/,"").split(",")[1].lstrip

end

def get_quality(file_path)

  quality = File.open(file_path).grep(/SetUseProxy/).first.match(/\".+\"/).to_a.first

end

# Get location of scripts

$renderQ = {}

puts "Where are the scripts?"
$TARGET = gets.rstrip
$TARGET << "/" if $TARGET !~ /\/$/

$script_list = %x| ls #{$TARGET}*.shk |.split("\n")   # Only files with *.shk extension

# Replace whitespace (if present) in filenames with underscores

$script_list.map! do |f|
    new_name = f.gsub(/\s/,'_')
    unless f == new_name
      puts "Renaming #{f} --> #{new_name}"
      File.rename(f,new_name)
    end
  new_name
end

# Make sure all renders are full-resolution

$script_list.each do |f|

  file,path,filename = File.open(f), File.dirname(f), File.basename(f).split(/\./)

  unless file.read.grep(/SetUseProxy/).first =~ /Base/
    
    new_file = File.open(f).read.gsub!(/^SetUseProxy.+;$/,'SetUseProxy("Base");')
    
    File.open(f,'w') { |f| f.puts new_file }

  end

end

# Get render info on scripts

$script_list.each do |f|
  outfile = get_output_file(f)
  last_frame = get_last_frame(f)
  quality = get_quality(f)
  $renderQ[f] = [last_frame,quality,outfile]
end

# Print report

puts "\n---------| RENDER SCRIPT -- FRAME COUNT -- OUTPUT FILE |---------\n"

$renderQ.sort.each { |k,v| puts "#{k} -- #{v.first} --> #{v.last} | #{v[1]}" }

puts "---------------------------------------------------"

puts "\nPress Enter to continue with render, or force exit."

gets

# Render

$renderQ.each { |k,v| render(k,v.first) }

« Newer Snippets
Older Snippets »
7 total  XML / RSS feed