Module: Alfred::Util

Defined in:
lib/alfred/util.rb

Class Method Summary collapse

Class Method Details

.escape_applescript(str) ⇒ Object

escape text for use in an AppleScript string



18
19
20
# File 'lib/alfred/util.rb', line 18

def escape_applescript(str)
  str.to_s.gsub(/(?=["\\])/, '\\')
end

.google(query) ⇒ Object



47
48
49
# File 'lib/alfred/util.rb', line 47

def google(query)
  open_url %Q{http://www.google.com/search?as_q=#{URI.escape(query)}&lr=lang_}
end

.make_webloc(name, url, folder = nil, comment = '') ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/alfred/util.rb', line 22

def make_webloc(name, url, folder=nil, comment = '')
  date = Time.now.strftime("%m-%d-%Y %I:%M%p")
  folder = Alfred.workflow_folder unless folder
  folder, name, url, comment = [folder, name, url, comment].map do |t|
    escape_applescript(t)
  end

  return %x{
  osascript << __APPLESCRIPT__
  tell application "Finder"
      set webloc to make new internet location file at (POSIX file "#{folder}") ¬
      to "#{url}" with properties ¬
      {name:"#{name}",creation date:(AppleScript's date "#{date}"), ¬
      comment:"#{comment}"}
  end tell
  return POSIX path of (webloc as string)
__APPLESCRIPT__}
end

.notify(query, message, opts = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/alfred/util.rb', line 88

def notify(query, message, opts = {})
  if Alfred::OSX.notification_center?
    notifier_options = {
      :title   => 'Alfred Notification'             ,
      :sound   => 'default'                         ,
      :execute => search_command(query)             ,
    }.merge!(opts)
    p notifier_options
    TerminalNotifier.notify(message, notifier_options)
  else
    system search_command(query)
  end
end

.open_url(url) ⇒ Object



42
43
44
45
# File 'lib/alfred/util.rb', line 42

def open_url(url)
  uri = URI.parse(url)
  %x{/usr/bin/open #{uri.to_s}}
end

.open_with(app, path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alfred/util.rb', line 51

def open_with(app, path)
  %x{osascript <<__APPLESCRIPT__
  tell application "#{app}"
      try
          open "#{path}"
          activate
      on error err_msg number err_num
          return err_msg
      end try
  end tell
__APPLESCRIPT__}
end

.reveal_in_finder(path) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/alfred/util.rb', line 64

def reveal_in_finder(path)
  raise InvalidArgument, "#{path} does not exist." unless File.exist? path
  %x{osascript <<__APPLESCRIPT__
  tell application "Finder"
      try
          reveal POSIX file "#{path}"
          activate
      on error err_msg number err_num
          return err_msg
      end try
  end tell
__APPLESCRIPT__}
end

.search_command(query = '') ⇒ Object



79
80
81
82
83
84
85
# File 'lib/alfred/util.rb', line 79

def search_command(query = '')
  %Q{osascript <<__APPLESCRIPT__
tell application "Alfred 2"
  search "#{escape_applescript(query)}"
end tell
__APPLESCRIPT__}
end