Skip to content

Instantly share code, notes, and snippets.

@ryanlfoster
Forked from nbqx/ind_server.rb
Created May 6, 2013 20:09
Show Gist options
  • Save ryanlfoster/5527775 to your computer and use it in GitHub Desktop.
Save ryanlfoster/5527775 to your computer and use it in GitHub Desktop.
%w(sinatra notify).each{|x| require x}
require "sinatra/reloader" if development?
configure do
TITLE = "InDesign Scripting Server"
INDESIGN_VERSION = "CS5"
end
get "/" do
Notify.notify TITLE, "Target Versiion InDesign #{INDESIGN_VERSION}"
content_type :text
"ok"
end
## curl -X POST -F 'script=alert("ok")' http://localhost:4567
post "/" do
##Notify.notify TITLE, "Execute Script"
scrt = params[:script]
create_javascript(scrt)
exec
content_type :text
"ok"
end
def exec
if File.exists? File.dirname(__FILE__)+"/tmp.log"
File.unlink File.dirname(__FILE__)+"/tmp.log"
end
create_dispatcher_applescript
`osascript #{File.dirname(__FILE__)+"/tmp.scpt"}`
end
def create_javascript(scrt)
open(File.dirname(__FILE__)+"/tmp.jsx","w"){|f| f.print scrt}
end
def create_dispatcher_applescript
override_js = <<JS
var file=new File('#{File.expand_path(File.dirname(__FILE__))+'/tmp.log'}');
file.encodign='UTF-8';file.lineFeed='Mac';file.open('w');
$.write=function(){file.seek(0,2);file.write(arguments[0])};
$.writeln=function(){file.seek(0,2);file.writeln(arguments[0])};
JS
scrt = <<SCRT
tell application \"Adobe InDesign #{INDESIGN_VERSION}\"
activate
do script \"#{override_js} $.evalFile('#{File.expand_path(File.dirname(__FILE__))+'/tmp.jsx'}')\" language javascript
end tell
SCRT
open(File.dirname(__FILE__)+"/tmp.scpt","w"){|f| f.print scrt}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment