-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFileMatcher.txt
executable file
·100 lines (89 loc) · 2.29 KB
/
FileMatcher.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl
# WARNING: you have to make this script executable
# for example 'chmod 0755 ~/Library/Application\ Scripts/com.onflapp.LinCastor-Browser/MyScript.txt'
#
# this script will get all URL 'variables' passed to the STDIN in format KEY="value"
# the variables may include:
#
# URL => my-http://myhost.domain.com:8080/mysite/a.html?search=blah#myanchor
# URL_SCHEME => my-http
# URL_HOST => myhost.domain.com
# URL_PORT => 8080
# URL_QUERY => ?search=blah
# URL_PATH => /mysite/a.html
# URL_FRAGMENT => #myanchor
# URL_VALUE => everything that comes after the 'scheme:'
# URL_B64VALUE => the same as URL_VALUE but decoded using based64
#
# TEXT => the selected text used for a lookup
#
# NOTE: The system env variable (e.g. PATH) may have different values
# then what you would expect from executing this script in the Terminal.
#
# ---
#
# you should exit with 0, this means the handler has finished successfully
# non-zero value indicates an error
$HOME = $ENV{'HOME'};
$TEXT = "";
$APP_ID = "";
sub open_or_reveal {
my ($f) = @_;
if (system ('open', $f) != 0) {
system ('open', '-R', $f);
}
}
while (<STDIN>) {
chomp();
if (m/^TEXT='(.*?)'$/) {
$TEXT=$1;
}
elsif (m/^APP_ID='(.*)'$/) {
$APP_ID=$1;
}
elsif (m/^URL_PATH_NAME='(.*)'$/) {
$TEXT=$1;
}
elsif (m/^URL_PATH='(.*)'$/) {
$TEXT=$1;
}
}
if ($TEXT =~ m/^file:\/\/(.*)$/) {
open_or_reveal($1);
exit 0;
}
elsif ($TEXT =~ m/(\/[a-zA-Z0-9_\/\-\.]+):(\d+)/) {
open_or_reveal ("mvim://open?url=file://$1&line=$2") if (-f $1 || -d $1);
exit 0;
}
$FILE = $TEXT;
$FILE =~ s/~/$HOME/;
$FILE =~ s/\\//g;
if (-e $FILE) {
open_or_reveal ($FILE) ;
exit 0;
}
if ($TEXT =~ /^\//) {
$TEXT = substr($TEXT, 1);
}
#$CURRENT_PATH=`osascript "$HOME/Library/Application Scripts/com.onflapp.LinCastor-Browser/GetPathToFrontmostDocument.scpt" "$APP_ID" `;
chomp($CURRENT_PATH);
if ($CURRENT_PATH ne "") {
if (-e "$CURRENT_PATH/$FILE" || -d "CURRENT_PATH/$FILE") {
open_or_reveal ("$CURRENT_PATH/$FILE") ;
exit 0;
}
elsif (-e "$CURRENT_PATH/../$FILE" || -d "CURRENT_PATH/../$FILE") {
open_or_reveal ("$CURRENT_PATH/../$FILE") ;
exit 0;
}
}
else {
$FILE = `mdfind -name "$TEXT" | head -1`;
chomp($FILE);
if (-e "$FILE") {
open_or_reveal ($FILE) ;
exit 0;
}
}
exit 1;