├── README.md ├── doc └── applescript.txt ├── ftdetect └── applescript.vim ├── ftplugin └── applescript.vim ├── indent └── applescript.vim ├── syntax └── applescript.vim └── test └── test.vimspec /README.md: -------------------------------------------------------------------------------- 1 | # Vim-applescript 2 | Syntax file from: http://www.fastnlight.com/syntax/applescript 3 | 4 | ## Usage 5 | ### Commands (for Mac) 6 | `:[range]AppleScriptRun` 7 | Execute current buffer as AppleScript. 8 | 9 | `:[range]AppleScriptExport` 10 | Export current buffer as AppleScript. 11 | 12 | ## Config 13 | `g:applescript_config.run.output.buffer_name` 14 | The output buffer's name used in `:AppleScriptRun` commmand. 15 | Default: `'[AppleScriptRun Output]'` 16 | 17 | `g:applescript_config.run.output.open_command` 18 | A command to open output buffer used in `:AppleScriptRun` command. 19 | Default: `'botright split'` 20 | -------------------------------------------------------------------------------- /doc/applescript.txt: -------------------------------------------------------------------------------- 1 | *applescript.txt* Utility scripts for applescript. 2 | 3 | ============================================================================== 4 | Contents *applescript-contents* 5 | 6 | Introduction ........................... |applescript-introduction| 7 | Commands ............................... |applescript-commands| 8 | Mappings ............................... |applescript-key-mappings| 9 | Config ............................... |applescript-config| 10 | 11 | ============================================================================== 12 | Introducton *applescript-introduction* *applescript.vim* 13 | 14 | This plugin provides following things: 15 | - Syntax (from http://www.fastnlight.com/syntax/applescript) 16 | - A command which executes current buffer. 17 | - A command which exports current buffer as an application. 18 | - A mapping which makes easy to input "¬". 19 | 20 | Latest Version: 21 | http://github.com/mityu/vim-applescript 22 | 23 | ============================================================================== 24 | Command *applescript-commands* 25 | 26 | *:AppleScriptRun* 27 | :[range]AppleScriptRun 28 | Execute current buffer. 29 | 30 | |:AppleScriptRun| can take |:range|. 31 | Default |:range| is "%". 32 | 33 | *:AppleScriptExport* 34 | :[range]AppleScriptExport 35 | Export current buffer to an application. 36 | 37 | |:AppleScriptExport| can take |:range|. 38 | Default |:range| is "%". 39 | 40 | ============================================================================== 41 | Mapping *applescript-key-mappings* 42 | 43 | *(applescript-line-connecting-CR)* 44 | (applescript-line-connecting-CR) 45 | This key mapping is provided on |Insert-mode|. 46 | This just inputs "¬" 47 | 48 | *applescript-default-mapping* 49 | Default mappings is NOT provided. 50 | 51 | When you define your mapping of these, you have to use |:imap| and |:map-|. 52 | 53 | Example: *applescript-key-mappings-example* 54 | > 55 | " Map (applescript-line-connecting-CR) to 56 | :imap (applescript-line-connecting-CR) 57 | 58 | < 59 | 60 | ============================================================================== 61 | Config *applescript-config* *g:applescript_config* 62 | 63 | You can define some options using |Dictionary| type variable, 64 | |g:applescript_config|. 65 | 66 | *g:applescript_config.run.output.buffer_name* 67 | g:applescript_config.run.output.buffer_name 68 | Set the output buffer's name. 69 | (Default: "[AppleScriptRun Output]") 70 | 71 | *g:applescript_config.run.output.open_command* 72 | g:applescript_config.run.output.open_command 73 | Set a command to open the output buffer. 74 | (Default: "botright split") 75 | 76 | Example: *g:applescript_config-example* 77 | > 78 | " Set the output buffer's name to "AppleScriptOutput", and 79 | " command to open output buffer to "vsplit" 80 | let g:applescript_config = { 81 | \ 'run' : { 82 | \ 'output' : { 83 | \ 'buffer_name' : 'AppleScriptOutput', 84 | \ 'open_command' : 'vsplit' 85 | \ } 86 | \ } 87 | \} 88 | < 89 | ============================================================================== 90 | vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl 91 | -------------------------------------------------------------------------------- /ftdetect/applescript.vim: -------------------------------------------------------------------------------- 1 | "Plugin Name: AppleScript 2 | "Author: mityu 3 | "Modified: idrisr 4 | "Last Change: 14-Jan-2022. 5 | 6 | let s:cpo_save=&cpo 7 | set cpo&vim 8 | 9 | au BufNewFile,BufRead *.scpt setf applescript 10 | au BufNewFile,BufRead *.applescript setf applescript 11 | au BufNewFile,BufRead * call s:checkshebang() 12 | 13 | function s:checkshebang() 14 | if !did_filetype() && getline(1) =~ '^#!.*osascript$' 15 | setfiletype applescript 16 | endif 17 | endfunction 18 | 19 | let &cpo=s:cpo_save 20 | unlet s:cpo_save 21 | 22 | " vim: foldmethod=marker 23 | -------------------------------------------------------------------------------- /ftplugin/applescript.vim: -------------------------------------------------------------------------------- 1 | "Plugin Name: applescript filetype plugin 2 | "Author: mityu 3 | "Last Change: 21-Jan-2022. 4 | 5 | scriptencoding utf-8 6 | 7 | if exists('b:did_ftplugin') 8 | finish 9 | endif 10 | let b:did_ftplugin=1 11 | 12 | let s:cpo_save=&cpo 13 | set cpo&vim 14 | 15 | if !exists('*ApplescriptFtpluginUndo') 16 | func ApplescriptFtpluginUndo() 17 | setlocal fo< commentstring< comments< 18 | silent! iunmap (applescript-line-connecting-CR) 19 | 20 | if exists(':AppleScriptRun') == 2 21 | delcommand -buffer AppleScriptRun 22 | endif 23 | 24 | if exists(':AppleScriptExport') == 2 25 | delcommand -buffer AppleScriptExport 26 | endif 27 | endfunc 28 | endif 29 | let b:undo_ftplugin = 'call ApplescriptFtpluginUndo()' 30 | 31 | inoremap (applescript-line-connecting-CR) ¬ 32 | 33 | setlocal fo-=t fo+=croql 34 | setlocal commentstring=--%s 35 | setlocal comments=sO:*\ -,mxO:*\ \ ,exO:*),s1:(*,mb:*,ex:*),:-- 36 | 37 | let s:default_config = {} 38 | let s:default_config.run = { 39 | \'output': { 40 | \ 'buffer_name': '[AppleScriptRun Output]', 41 | \ 'open_command': 'botright split' 42 | \ } 43 | \} 44 | func! s:bufnr(expr) abort "{{{ 45 | if type(a:expr) == type('') 46 | return bufnr(escape(a:expr,'\/[]^$*.?')) 47 | else 48 | return a:expr 49 | endif 50 | endfunc "}}} 51 | func! s:bufexists(expr) abort "{{{ 52 | return s:bufnr(a:expr) != -1 53 | endfunc "}}} 54 | func! s:bufexists_on_this_tab(expr) abort "{{{ 55 | return s:bufexists(a:expr) && bufwinnr(s:bufnr(a:expr)) != -1 56 | endfunc "}}} 57 | func! s:goto_win(expr) abort "{{{ 58 | execute bufwinnr(s:bufnr(a:expr)) 'wincmd w' 59 | endfunc "}}} 60 | func! s:tempfile() abort "{{{ 61 | return tempname() . '.applescript' 62 | endfunc "}}} 63 | 64 | if executable('osascript') 65 | com! -range=% -buffer AppleScriptRun call s:runAppleScript(,) 66 | 67 | func! s:runAppleScript(start,end) abort "{{{ 68 | if exists('g:applescript_config') && has_key(g:applescript_config,'run') 69 | let config = extend(deepcopy(g:applescript_config.run),s:default_config.run,'keep') 70 | else 71 | let config = deepcopy(s:default_config.run) 72 | endif 73 | let current_bufnr = bufnr('%') 74 | let script = getline(a:start,a:end) 75 | let tempfile = s:tempfile() 76 | let cmd = printf('osascript %s',shellescape(tempfile)) 77 | 78 | try 79 | call writefile(script,tempfile) 80 | 81 | if s:bufexists_on_this_tab(config.output.buffer_name) 82 | call s:goto_win(config.output.buffer_name) 83 | else 84 | execute config.output.open_command config.output.buffer_name 85 | setlocal buftype=nofile nobuflisted noswapfile noundofile ft=AppleScriptRunOutput 86 | endif 87 | 88 | let output = system(cmd) 89 | silent %delete _ 90 | 0put =output 91 | catch 92 | echohl Error 93 | echomsg v:exception 94 | echohl None 95 | return 96 | finally 97 | call delete(tempfile) 98 | call s:goto_win(current_bufnr) 99 | endtry 100 | endfunc "}}} 101 | endif 102 | 103 | if executable('osacompile') 104 | com! -buffer -range=% AppleScriptExport call s:exportAppleScript(,) 105 | 106 | func! s:ask(msg) abort "{{{ 107 | return input(a:msg . ' (Y[es]/N[o]) ') =~? 'y' 108 | endfunc "}}} 109 | func! s:exportAppleScript(start,end) abort "{{{ 110 | let script = getline(a:start,a:end) 111 | let tempfile = s:tempfile() 112 | let product_name = '' 113 | let config = {} 114 | let config.execute_only = {'flag': 0, 'argv': '-x'} 115 | let config.stay_open = {'flag': 0, 'argv': '-s'} 116 | let config.use_startup_screen = {'flag': 0, 'argv': '-u'} 117 | 118 | while product_name ==# '' 119 | let product_name = input( 120 | \"Input export file name (*.scpt=script, *.scptd=script bundle, *.app=application bundle)\n>> ", 121 | \'','dir') 122 | endwhile 123 | let config.execute_only.flag = s:ask('Export execute only') 124 | if product_name =~? '.\+\.app$' 125 | let config.stay_open.flag = s:ask('Stay-open applet') 126 | let config.use_startup_screen.flag = s:ask('Use startup screen') 127 | endif 128 | 129 | if getftype(product_name) !=# '' 130 | if !s:ask(printf('File "%s" exists. Overwrite?',product_name)) 131 | echo 'Canceled.' 132 | return 133 | endif 134 | endif 135 | 136 | let flags = join(values(map(filter(config,'v:val.flag'),'v:val.argv')),' ') 137 | let cmd = printf('osacompile -o %s %s %s',product_name,flags,tempfile) 138 | 139 | try 140 | call writefile(script,tempfile) 141 | let output = system(cmd) 142 | 143 | if output ==# '' 144 | echo printf('Export "%s" successfully.',product_name) 145 | else 146 | echohl Error 147 | echomsg '[command]' cmd 148 | echomsg output 149 | echohl None 150 | echomsg '[You can read these message again by executing ":messages"]' 151 | endif 152 | catch 153 | echohl Error 154 | echomsg v:exception 155 | echohl None 156 | finally 157 | call delete(tempfile) 158 | endtry 159 | endfunc "}}} 160 | endif 161 | 162 | let &cpo=s:cpo_save 163 | unlet s:cpo_save 164 | 165 | " vim: foldmethod=marker 166 | -------------------------------------------------------------------------------- /indent/applescript.vim: -------------------------------------------------------------------------------- 1 | "Plugin Name: applescript indent file. 2 | "Author: mityu 3 | "Last Change: 02-May-2017. 4 | 5 | let s:cpo_save=&cpo 6 | set cpo&vim 7 | 8 | setlocal indentexpr=GetAppleScriptIndent() 9 | setlocal indentkeys+=0=end,0=else,=error 10 | 11 | func! GetAppleScriptIndent() 12 | let l:ignorecase_save=&ignorecase 13 | try 14 | let &ignorecase=0 15 | return s:returnAppleScriptIndent() 16 | finally 17 | let &ignorecase=l:ignorecase_save 18 | endtry 19 | endfunc 20 | 21 | func! s:returnAppleScriptIndent() 22 | let l:current_text=getline(v:lnum) 23 | 24 | let l:prev_line=prevnonblank(v:lnum-1) 25 | 26 | "At the start of the file, use 0 indent. 27 | if l:prev_line==0 28 | return 0 29 | endif 30 | 31 | let l:prev_line_save=l:prev_line 32 | let l:prev_line=s:prev_non_connected_line(l:prev_line) 33 | 34 | let l:indent=indent(l:prev_line) 35 | 36 | if l:prev_line_save-l:prev_line==1 37 | "連結開始 38 | let l:indent+=shiftwidth()*2 39 | elseif l:prev_line_save-l:prev_line>=2 40 | "絶賛連結中 41 | "その時は前の行のインデントをそのまま流用する 42 | return indent(l:prev_line_save) 43 | elseif l:prev_line_save==l:prev_line && s:doesOrderConnect(getline(l:prev_line-1)) 44 | "前の行が連結される行の最終行の場合 45 | let l:prev_line=s:prev_non_connected_line(l:prev_line-1) 46 | if l:prev_line==0 | let l:prev_line=1 | endif 47 | let l:indent=indent(l:prev_line) 48 | endif 49 | 50 | let l:prev_text=getline(l:prev_line) 51 | if l:prev_text=~'^\s*\(on\|\(tell\(.*\\)\@!\)\|repeat\|try\|if\|else\)' 52 | let l:indent+=shiftwidth() 53 | endif 54 | 55 | if l:current_text=~'^\s*\(end\|else\|on\serror\)' 56 | let l:indent-=shiftwidth() 57 | endif 58 | 59 | return l:indent 60 | endfunc 61 | 62 | func! s:prev_non_connected_line(line) 63 | let l:prev_line=prevnonblank(a:line) 64 | while l:prev_line>0 && s:doesOrderConnect(getline(l:prev_line)) 65 | let l:prev_line-=1 66 | endwhile 67 | return l:prev_line 68 | endfunc 69 | 70 | func! s:doesOrderConnect(text) 71 | return a:text=~'¬$' 72 | endfunc 73 | 74 | let &cpo=s:cpo_save 75 | unlet s:cpo_save 76 | 77 | " vim: foldmethod=marker 78 | -------------------------------------------------------------------------------- /syntax/applescript.vim: -------------------------------------------------------------------------------- 1 | " Vim syntax file 2 | " Language: AppleScript 3 | " Maintainer: Jim Eberle 4 | " Last Change: Mar 18, 2010 5 | " URL: http://www.fastnlight.com/syntax/applescript.vim 6 | 7 | " Use :syn w/in a buffer to see language element breakdown 8 | 9 | if version < 600 10 | syntax clear 11 | elseif exists("b:current_syntax") 12 | finish 13 | endif 14 | 15 | " --- Statement --- 16 | syn keyword scptStmt get set count copy run global local prop property 17 | syn keyword scptStmt close put delete duplicate exists 18 | syn keyword scptStmt launch open print quit make move reopen save 19 | syn keyword scptStmt saving into 20 | hi def link scptStmt Statement 21 | 22 | " --- Type --- 23 | syn keyword scptType text string number integer real color date 24 | hi def link scptType Type 25 | 26 | " --- Operator --- 27 | syn keyword scptOp div mod not and or as 28 | syn match scptOp "[-+*/^&]" 29 | " MacRoman single char :- (divide) 30 | exec 'syn match scptOp "'.nr2char(214).'"' 31 | syn match scptOp "\<\(a \)\?\(ref\( to\)\?\|reference to\)\>" 32 | hi def link scptOp Operator 33 | 34 | " Containment 35 | syn match scptIN "\" 36 | syn match scptIN "\" 37 | syn match scptIN "\" 38 | syn match scptIN "\" 39 | syn match scptIN "\" 40 | syn match scptIN "\" 41 | syn match scptIN "\" 42 | syn match scptIN "\" 43 | syn match scptIN "\" 44 | syn match scptIN "\" 45 | syn match scptIN "\" 46 | hi def link scptIN scptOp 47 | 48 | " Equals 49 | syn match scptEQ "=" 50 | syn match scptEQ "\" 51 | syn match scptEQ "\" 52 | syn match scptEQ "\" 53 | syn match scptEQ "\" 54 | syn match scptEQ "\" 55 | hi def link scptEQ scptOp 56 | 57 | " Not Equals 58 | syn match scptNE "\" 59 | syn match scptNE "\" 60 | syn match scptNE "\" 61 | syn match scptNE "\" 62 | syn match scptNE "\" 63 | syn match scptNE "\" 64 | hi def link scptNE scptOp 65 | " MacRoman single char /= 66 | exec 'syn match scptNE "'.nr2char(173).'"' 67 | 68 | " Less Than 69 | syn match scptLT "<" 70 | syn match scptLT "\" 71 | syn match scptLT "\(is \)\?less than" 72 | syn match scptLT "\" 73 | syn match scptLT "\" 74 | hi def link scptLT scptOp 75 | 76 | " Greater Than 77 | syn match scptGT ">" 78 | syn match scptGT "\" 79 | syn match scptGT "\(is \)\?greater than" 80 | syn match scptGT "\" 81 | syn match scptGT "\" 82 | hi def link scptGT scptOp 83 | 84 | " Less Than or Equals 85 | syn match scptLE "<=" 86 | syn match scptLE "\" 87 | syn match scptLE "\" 88 | syn match scptLE "\(is \)\?less than or equal\( to\)\?" 89 | syn match scptLE "\" 90 | syn match scptLE "\" 91 | hi def link scptLE scptOp 92 | " MacRoman single char <= 93 | exec 'syn match scptLE "'.nr2char(178).'"' 94 | 95 | " Greater Than or Equals 96 | syn match scptGE ">=" 97 | syn match scptGE "\" 98 | syn match scptGE "\" 99 | syn match scptGE "\(is \)\?greater than or equal\( to\)\?" 100 | syn match scptGE "\" 101 | syn match scptGE "\" 102 | hi def link scptGE scptOp 103 | " MacRoman single char >= 104 | exec 'syn match scptGE "'.nr2char(179).'"' 105 | 106 | " --- Constant String --- 107 | syn region scptString start=+"+ skip=+\\\\\|\\"+ end=+"+ 108 | hi def link scptString String 109 | 110 | " --- Constant Number --- 111 | syn match scptNumber "\<-\?\d\+\>" 112 | hi def link scptNumber Number 113 | 114 | " --- Constant Float --- 115 | syn match scptFloat display contained "\d\+\.\d*\(e[-+]\=\d\+\)\=" 116 | syn match scptFloat display contained "\.\d\+\(e[-+]\=\d\+\)\=\>" 117 | syn match scptFloat display contained "\d\+e[-+]\>" 118 | hi def link scptFloat Float 119 | 120 | " --- Constant Boolean --- 121 | syn keyword scptBoolean true false yes no ask 122 | hi def link scptBoolean Boolean 123 | 124 | " --- Other Constants --- 125 | syn keyword scptConst it me version pi result space tab anything 126 | syn match scptConst "\" 127 | 128 | " Considering and Ignoring 129 | syn match scptConst "\" 130 | syn match scptConst "\" 131 | syn match scptConst "\" 132 | syn keyword scptConst case diacriticals expansion hyphens punctuation 133 | hi def link scptConst Constant 134 | 135 | " Style 136 | syn match scptStyle "\" 137 | syn match scptStyle "\" 138 | syn match scptStyle "\" 139 | syn keyword scptStyle bold condensed expanded hidden italic outline plain 140 | syn keyword scptStyle shadow strikethrough subscript superscript underline 141 | hi def link scptStyle scptConst 142 | 143 | " Day 144 | syn keyword scptDay Mon Tue Wed Thu Fri Sat Sun 145 | syn keyword scptDay Monday Tuesday Wednesday Thursday Friday Saturday Sunday 146 | syn keyword scptDay weekday 147 | hi def link scptDay scptConst 148 | 149 | " Month 150 | syn keyword scptMonth Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 151 | syn keyword scptMonth January February March 152 | syn keyword scptMonth April May June 153 | syn keyword scptMonth July August September 154 | syn keyword scptMonth October November December 155 | syn keyword scptMonth month 156 | hi def link scptMonth scptConst 157 | 158 | " Time 159 | syn keyword scptTime minutes hours days weeks 160 | hi def link scptTime scptConstant 161 | 162 | " --- Conditional --- 163 | syn keyword scptCond if then else 164 | syn match scptCond "\" 165 | hi def link scptCond Conditional 166 | 167 | " --- Repeat --- 168 | syn keyword scptRepeat repeat with from to by continue 169 | syn match scptRepeat "\" 170 | syn match scptRepeat "\" 171 | syn match scptRepeat "\" 172 | syn match scptRepeat "\" 173 | hi def link scptRepeat Repeat 174 | 175 | " --- Exception --- 176 | syn keyword scptException try error 177 | syn match scptException "\" 178 | syn match scptException "\" 179 | syn match scptException "\" 180 | hi def link scptException Exception 181 | 182 | " --- Keyword --- 183 | syn keyword scptKeyword end tell times exit 184 | syn keyword scptKeyword application file alias activate 185 | syn keyword scptKeyword script on return without given 186 | syn keyword scptKeyword considering ignoring items delimiters 187 | syn keyword scptKeyword some each every whose where id index item its 188 | syn keyword scptKeyword first second third fourth fifth sixth seventh 189 | syn keyword scptKeyword eighth ninth tenth container 190 | syn match scptKeyword "\d\+\(st\|nd\|rd\|th\)" 191 | syn keyword scptKeyword last front back middle named thru through 192 | syn keyword scptKeyword before after in of the 193 | syn match scptKeyword "\" 194 | syn match scptKeyword "\" 195 | syn match scptKeyword "\" 196 | syn match scptKeyword "\" 197 | syn match scptKeyword "\" 198 | syn match scptKeyword "\" 199 | syn match scptKeyword "\" 200 | syn match scptKeyword "\" 201 | syn match scptKeyword "\" 202 | syn match scptKeyword "'s" 203 | hi def link scptKeyword Keyword 204 | 205 | " US Units 206 | syn keyword scptUnitUS quarts gallons ounces pounds inches feet yards miles 207 | syn match scptUnitUS "\" 208 | syn match scptUnitUS "\" 209 | syn match scptUnitUS "\" 210 | syn match scptUnitUS "\" 211 | syn match scptUnitUS "\" 212 | syn match scptUnitUS "\" 213 | syn match scptUnitUS "\" 214 | hi def link scptUnitUS scptKey 215 | 216 | " British Units 217 | syn keyword scptUnitBT litres centimetres metres kilometres 218 | syn match scptUnitBT "\" 219 | syn match scptUnitBT "\" 220 | syn match scptUnitBT "\" 221 | syn match scptUnitBT "\" 222 | hi def link scptUnitBT scptKey 223 | 224 | " Metric Units 225 | syn keyword scptUnitMT liters centimeters meters kilometers grams kilograms 226 | syn match scptUnitMT "\" 227 | syn match scptUnitMT "\" 228 | syn match scptUnitMT "\" 229 | syn match scptUnitMT "\" 230 | syn match scptUnitMT "\" 231 | syn match scptUnitMT "\" 232 | hi def link scptUnitMT scptKey 233 | 234 | " --- Comment --- 235 | syn match scptComment "--.*" 236 | syn match scptComment "#.*" 237 | syn region scptComment start="(\*" end="\*)" 238 | hi def link scptComment Comment 239 | 240 | " --- Todo --- 241 | syn keyword scptTodo contained TODO FIXME XXX 242 | hi def link scptTodo Todo 243 | 244 | let b:current_syntax = "applescript" 245 | 246 | -------------------------------------------------------------------------------- /test/test.vimspec: -------------------------------------------------------------------------------- 1 | call themis#helper('command').with(themis#helper('assert')) 2 | 3 | Describe ftdetect-applescript 4 | Before all 5 | runtime! ftdetect/applescript.vim 6 | End 7 | 8 | Before each 9 | %bwipeout! 10 | End 11 | 12 | It sets filetype for *.scpt files 13 | e a.scpt 14 | Assert Equals(&filetype, 'applescript') 15 | End 16 | 17 | It sets filetype for *.applescript files 18 | e a.applescript 19 | Assert Equals(&filetype, 'applescript') 20 | End 21 | 22 | It sets filetype if 'osascript' appears in shebang 23 | let tmpfile = tempname() . '.sh' 24 | call writefile(['#!/ust/bin/osascript'], tmpfile) 25 | try 26 | execute 'e' fnameescape(tmpfile) 27 | Assert Equals(&filetype, 'applescript') 28 | finally 29 | call delete(tmpfile) 30 | endtry 31 | End 32 | End 33 | --------------------------------------------------------------------------------