Skip to content

Commit

Permalink
Early.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy authored and Tommy committed Jan 3, 2011
1 parent f957890 commit 92b4af1
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 4 deletions.
63 changes: 59 additions & 4 deletions devonthink_helper.rb
Expand Up @@ -13,13 +13,68 @@

class Devonthink_helper
def initialize(database)
@log = Log.new(__FILE__) # Logs are kept in ~/Library/Logs/Ruby/DevonThink_helper
sleep(1) # Console has trouble when new logs are created to quickly. TODO Move to 'log' module.
@created_deleted_log = Log.new("Created & deleted items")
begin # logs
@log = Log.new(__FILE__) # Logs are kept in ~/Library/Logs/Ruby/DevonThink_helper
sleep(1) # Console has trouble when new logs are created to quickly. TODO Move to 'log' module.
@created_deleted_log = Log.new("Created & deleted items")
sleep(1)
@walker_log = Log.new('Walker') # Follows the walk of the iterators

end
begin # DevonThink items
@devonthink = SBApplication.applicationWithBundleIdentifier_('com.devon-technologies.thinkpro2')
@db = @devonthink.databases.select{|db| db.name == database}[0].get
end

@textedit = SBApplication.applicationWithBundleIdentifier_('com.apple.TextEdit')

begin # Tommys stuff. TODO Remove
@ab = @db.root.children.select{|c| c.name == 'Användbarhetsboken'}[0].get
@ab2 = @db.root.children.select{|c| c.name == 'Användbarhetsboken 2'}[0].get
@tillf = @db.root.children.select{|c| c.name == 'tillf'}[0].get
end
end

begin # Help-functions
begin # Iterators

# Main iterator
# Will yield items from inbox and other user created groups, but not from Smart Groups, Trash etc.
def each_normal_group_record(top, wide_deep = :deep, level=0)
# wide_deep = :wide is not implemented yet
level += 1
top = top.get # I'm using a lot of .get, to avoid mysterious bugs (at the cost of a slower application)
@walker_log.debug " "*(level-1) + "'#{top.name}' (#{top.kind})"
yield(top)
end

def each_safe_record

end
end

begin
# Takes a string/symbol that points at a group, and returns the group.
# An empty string will return root.
def group_from_string(group_path)
# Get the context group
if group_path == '' or group_path == :root then
group = @db.root
elsif group_path == :inbox then
group = @db.incomingGroup
else
group = @devonthink.getRecordAt_in_(group_path, @db)
end
raise "No group with path: #{group_path}" unless group

return group
end
end
end
end # class Devonthink_helper

if __FILE__ == $0 then
dtd = Devonthink_helper.new('BokmarktPA04_TEST')
dtdb = Devonthink_helper.new('BokmarktPA04_TEST')
group = dtdb.group_from_string(group_path)
dtdb.each_normal_group_record('')
end
217 changes: 217 additions & 0 deletions pathstring.rb
@@ -0,0 +1,217 @@
#
# Pathstring.rb
# ItunesFeeder
#
# Created by Tommy Sundström on 25/2-09.
# Copyright (c) 2009 Helt Enkelt ab. All rights reserved.
#

require 'pathname'
require 'pp'
require 'osx/cocoa'


# Pathstring is a replacement for Pathname, that is a subclass to string. This way we get rid of the need
# to add to_s ever so often.
#
# Other differences:
#
# * It always expands ~-paths.
# * Some extra utility classes.
#
class Pathstring < String
def initialize(path)
# I'm not 100% about these two, so for the moment they'll have to go
# path = File.expand_path(path) if path[0].to_s == '~' # Auto-expand ~-paths
# path = File.expand_path(path) if path[0].to_s == '.' # Auto-expand paths anchored in present working directory

self.replace(path.to_s) # to_s in case it is a Pathname
@pathname = Pathname.new(path)
end

def method_missing(symbol, *args)
result = @pathname.__send__(symbol, *args) # BUG BUG BUG Ibland ger pathname andra sorters svar, t.ex. sant/falsk eller en array
# När det inte är en Pathname objekt jag får tillbaka, måste jag släppa vidare svaret som det är (typ)
# Fast ev kolla på innehållet i arrayen (t.ex. när det är children, och Pathstringa dem.
if result.class == Pathname then
return Pathstring.new(result)
elsif result.class == Array then
# If the members of the array is Pathnames, then they should be converted to Pathstrings
return result.collect do |t|
if t.class == Pathname then
Pathstring.new(t)
else
t
end
end
else
return result # Other kinds of results are returned as they are
end
end

def +(path) # Overrides String behaviour.
return Pathstring.new( (@pathname + Pathname.new(path)) )
end

# Differs from Pathname mkpath in that it also handles file paths
def mkpath
path = self.expand_path
path = path.dirname if path.file? # Make sure the directory for the file exists
Pathname(path).mkpath
end


# Added functions

# Returns the content of the file
# TODO Add error handling in case file is alreay open
def read
return File.open(self, 'r'){|f| f.read}
end

# Replaces the content of the file
# TODO Add error handling in case file is alreay open
def write(new_content)
File.open(self, 'w') {|f| f.write(new_content) }
end

# Like Dir.mkdir, but without the error if a folder is already in place
# The most common error is SystemCallError = The direcotry cannot be created
def ANVANDS_EJensure_directory # ANVÄND MKPATH ISTÄLLET!!!!
#$log.debug "Enters ensure_directory. self: #{self}"
if self.exist? then
#$log.debug "#{self} alredy existed"
return # Directory already in place, no need to do anything.
end
self.mkdir
end

# Returns the path of the volume
# Quite Mac-centric I'm afraid
def volume
path_parts = self.split(/\//) # Split on forward slash
# (Note that path_parts[0] is the empty string before the first '/'
if path_parts[1] == "Volumes" then
volume = Pathstring.new(path_parts[0..2].join('/')) # /Volumes/volumename
else
volume = Pathstring.new('/') # /
end
end

def rootvolume?
return self.volume == '/' ? true : false
end

# Checks if a volume exists (i.e. is mounted)
def mounted?
return File.exists?(volume) ? true : false
end

# Cheks if two paths are on the same volume
def same_volume?(path2)
return volume == Pathstring.new(path2).volume ? true : false # (Yes, I know this is tautologic; but it makes the code easer to read, at least for me)
end

# Moves the file.
# Unlike FileUtils.mv this can move across volumes.
# Can not move a directory
##def mv(destination)
##end

# If self is a directory, adds item.
# If something with the same name is already present, adds a number to item and tries again, until success or to many tries.
def enumbered_add_to_directory(item)
$log.info "Renamed #{Pathstring.new(item).basename} to xxxx"
end

# Array of child-files and folders that to not begin their name with a dot
def undotted_children
self.children.reject {|t| t[0].to_s == '.' }
end

def children_that_match(array_of_regexps)
# TODO:
end

def children_that_dont_match(array_of_regexps)
# TODO:
end

# Array of child-files and folders that to not begin their name with a dot
def children_except_those_beginning_with(array_of_beginnings)
# NOT IMPLEMENTED YET self.children.select {|t| t[0].to_s != '.' }
end

# Array of siblings (not including self)
def siblings
self.parent.children.reject {|t| t == self }
end


# Removes the extension from the basename
# Counterpart to extname
def basename_sans_ext
return File.basename(self, self.extname)
end

# Removes the .DS_Store file - an autocreated file that just contains the visual settings
# for the folder - if there is one. Note that it may quickly be recreated by OSX.
# Mac-centric
def delete_dsstore!
if (self + '.DS_Store').exist? then
(self + '.DS_Store').delete
end
return self
end

# Content of a directory, but with .DS_Store file excluded
def children_sans_dsstore
return self.children.reject{|t| t.basename == '.DS_Store'}
end

# Find the name for the application that contains this object
# Does this by finding the rb_main.rb-file in a Resource directory or the .app package.
# I'm not certain how robust this is. It's certainly RubyCocoa-centric.
#
def application_name
current = self.expand_path
until current.root?
if current.siblings.collect {|r| r.basename}.include?('rb_main.rb') then
if current.parent.basename == 'Resources' && current.parent.parent.basename == 'Contents'
# Part of an application bundle, applicationname.app
OSX::NSLog "application_name (appbundle): #{current.parent.parent.parent.basename_sans_ext}"
return current.parent.parent.parent.basename_sans_ext
else
# Assuming that we now are in the project folder, and that it is named like the app
#OSX::NSLog "application_name (raw): #{current.parent.basename}"
return current.parent.basename
end
end
current = current.parent # Up one level for the next round in the loop
end
raise 'Unable to find an application name.'
end

# Array of the basenames of the children TROR DETTA REDAN ÄR TÄCKT AV TYP Dir*
def children_basename

end

end

# This makes Pathstring(path) work as Pathstring.new(path)
# TODO: Refactor the whole Pathstring module to Path
module Kernel
# create a pathstring object.
#
def Pathstring(path) # :doc:
# require 'pathstring'
Pathstring.new(path)
end

def Path(path)
Pathstring.new(path)
end

private :Pathstring
end
1 change: 1 addition & 0 deletions rb_main.rb
@@ -0,0 +1 @@
# Dummy file, just here so that pathstring shall know what is the root directory.

0 comments on commit 92b4af1

Please sign in to comment.