Skip to content

Commit

Permalink
fix: cannot escape space in filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
willbchang committed Oct 19, 2020
1 parent d3e3d99 commit 01cf84e
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions script.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'shellwords'

def get_filepath(query)
# Remove single quote around file path when selecting from Alfred File Browser
/^'.*'/.match?(query) ? query[1..-2] : query
Expand All @@ -13,11 +15,15 @@ def get_script(query, runtimes)
filetype = get_filetype(filepath)

if File.directory?(filepath)
"cd #{filepath}"
# The \ in filepath has to be escaped third time so that it will actually work.
# The first time is in ruby string \\\\\\\\ becomes \\\\
# The second time is in apple script \\\\ becomes \\
# The third time is in Terminal app \\ becomes \ which will escape the special character(s).
"cd #{filepath.shellescape.gsub('\\', '\\\\\\\\')}"
elsif File.file?(filepath)
"#{runtimes[filetype]} #{filepath}"
else
query.gsub(/^\s*\$\s*/, '')
query.gsub(/^\s*\$\s*/, '')
end
end

Expand Down

0 comments on commit 01cf84e

Please sign in to comment.