-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbeditjslint.applescript
More file actions
166 lines (135 loc) · 5.54 KB
/
bbeditjslint.applescript
File metadata and controls
166 lines (135 loc) · 5.54 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
-- Based on John Gruber's PHP Syntax Checking script
-- from Daring Fireball
-- http://daringfireball.net/2003/12/php_syntax_checking_in_bbedit
-- Requires JavaScript Lint
-- http://www.javascriptlint.com/
-- Assumes it is installed at /usr/local/bin/jsl
-- requires OS >=10.4
-- Dual-licensed under the BSD or MIT licenses.
-- http://www.schmoyer.com/code/js_lint/license.phtml
on run
-- The run handler is called when the script is invoked normally,
-- such as from BBEdit's Scripts menu.
JSLint()
end run
on menuselect()
-- The menuselect() handler gets called when the script is invoked
-- by BBEdit as a menu script. Save this script, or an alias to it,
-- as "JS Lint Document" in the "Menu Scripts" folder in your
-- "BBEdit Support" folder.
JSLint()
end menuselect
on JSLint()
-- Set any specific JSL settings here
-- Make sure -process is LAST
-- More info: http://www.javascriptlint.com/docs/index.htm
set jsl to ¬
" /usr/local/bin/jsl " & ¬
"-nologo -output-format \"__LINE__|__ERROR__\"" & ¬
" -context -nosummary -nofilelisting " & ¬
" -process " as text
tell application "BBEdit"
try
set w to text window 1
on error
beep
return
end try
set the_filename to name of (document of w)
try
set current_file to file of text window 1
on error
display alert "Please save this file first."
return
end try
if (modified of (document of w)) then
-- unsaved changes, write a temp file
set parent_folder to (path to temporary items folder)
set temp_file to (parent_folder as string) & the_filename
if not (my WriteUnixTextFile(temp_file, text of w)) then
-- End script, because an error occured writing the temp file
return
end if
else
-- use the real file on disk
tell application "Finder"
set parent_folder to (container of (file of w as alias)) ¬
as alias
end tell
end if
end tell
set the_command to "cd " & quoted form of POSIX path of parent_folder ¬
& ";" & jsl & (quoted form of (the_filename))
set no_errors to true
try
-- jsl will have an exit status that is not zero
-- unless there are no Warnings or errors
set the_result to do shell script the_command
on error err_text
set no_errors to false
-- the actual error information is stored in err_text
-- since exit status is not 0
set the_result to err_text
end try
tell application "BBEdit"
if no_errors then
-- requires OS >=10.4
display alert "JSLint Results" message ¬
"No errors or warnings found in " & the_filename
else
set error_list to {}
repeat with the_line in (every paragraph of the_result)
if (the_line as text) is not equal to "" then
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "|"
set err_description to text item 2 of the_line
set line_num to text item 1 of the_line as integer
set AppleScript's text item delimiters to old_delims
if err_description contains "error" then
set err_kind to error_kind
else
set err_kind to warning_kind
end if
set err_list_item to ¬
{result_kind:¬
err_kind, result_file:¬
current_file, result_line:¬
line_num, message:¬
err_description as text}
copy err_list_item to end of error_list
end if
end repeat
make new results browser with data error_list ¬
with properties {name:"JSLint Results for " & the_filename}
end if
end tell
end JSLint
on WriteUnixTextFile(file_name, file_contents)
-- Write a text file with unix-style line endings.
-- Input:
-- file_name - the HFS-style path for the file to write
-- file_contents - the text to write to a file
-- Returns: true for success, false for failure
try
set file_ref to ¬
open for access file file_name with write permission
set eof of file_ref to 0
-- change the text of file_contents to unix line breaks
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set text_list to every text item of file_contents as list
set AppleScript's text item delimiters to (ASCII character 10)
set file_contents to (text_list as string)
set AppleScript's text item delimiters to old_delims
write file_contents to file_ref starting at eof
close access file_ref
return true
on error err_msg
try
close access file file_ref
end try
display dialog err_msg with icon stop buttons {"OK"} ¬
default button 1
return false
end try
end WriteUnixTextFile