将多个 Applescript 变量传递给 Ruby
Pass Multiple Applescript Variables to Ruby
我正在编写一个 Ruby 脚本,它需要来自 Applescript 的一些变量。现在我一次成功地抓住了他们,但它看起来不优雅而且可能很慢。例如:
note = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
if notebook of item 1 of the_selection is (notebook named "Writing") then
return HTML content of item 1 of the_selection
end if
else
return ""
end if
end tell
APPLESCRIPT}
title = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
if notebook of item 1 of the_selection is (notebook named "Writing") then
return title of item 1 of the_selection
end if
else
return ""
end if
end tell
APPLESCRIPT}
我知道我可能忽略了一些明显的东西,但是有没有一种简单的方法我可以只用一个 Applescript 片段来做到这一点 returns 两个变量都变成 Ruby (注意,标题)?
从 AppleScript 中获取多个值是比较容易的部分。 return 不是 return 单个值,而是 return 一个列表:
result = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
if notebook of item 1 of the_selection is (notebook named "Writing") then
set title to title of item 1 of the_selection
set html to HTML content of item 1 of the_selection
return { title, html }
end if
else
return ""
end if
end tell
APPLESCRIPT}
困难的部分是解析输出。假设你的 title
是 This is a note
并且 html
是 <h1>Hello World</h1>
,osascript
将 return 这个:
This is a note, <h1>Hello World</h1>
您可以在 ,
上拆分它,但如果 title
恰好包含一个逗号,您就有问题了。您还可以将 -ss
选项传递给 osascript
,这使其成为 return 格式的 AppleScript objects,但您不想在 Ruby.
如果您知道 title
永远不会包含换行符,另一种方法是在标题后添加一个换行符:
result = %x{ osascript <<APPLESCRIPT
...
if notebook of item 1 of the_selection is (notebook named "Writing") then
set title to title of item 1 of the_selection
set html to HTML content of item 1 of the_selection
return title & "\n" & html
end if
...
APPLESCRIPT}.chomp
现在输出 (result
) 将如下所示:
This is a note
<h1>Hello World</h1>
...您可以这样获得标题和 HTML:
title, html = result.split("\n", 2)
puts title
# => This is a note
puts html
# => <h1>Hello World</h1>
现在,如果您的任何标题中有换行符(我不记得 Evernote 是否允许这样做),或者如果您想 return 超过两个值,这将是有问题的以及。下一个最简单的解决方案是选择某种不太可能出现在输出的任何部分的定界符,例如 %!%
:
DELIMITER = '%!%'
result = %x{ osascript <<APPLESCRIPT
...
if notebook of item 1 of the_selection is (notebook named "Writing") then
set title to title of item 1 of the_selection
set html to HTML content of item 1 of the_selection
return title & "#{DELIMITER}" & html
end if
...
APPLESCRIPT}.chomp
# =>This is a note%!%<h1>Hello World</h1>
title, html = result.split(DELIMITER, 2)
如果一切都失败了,您可以使用插件使 osascript
输出一种 Ruby 知道如何解析的已知格式。刚才我发现这个免费的 JSON Helper 看起来很方便。
我正在编写一个 Ruby 脚本,它需要来自 Applescript 的一些变量。现在我一次成功地抓住了他们,但它看起来不优雅而且可能很慢。例如:
note = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
if notebook of item 1 of the_selection is (notebook named "Writing") then
return HTML content of item 1 of the_selection
end if
else
return ""
end if
end tell
APPLESCRIPT}
title = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
if notebook of item 1 of the_selection is (notebook named "Writing") then
return title of item 1 of the_selection
end if
else
return ""
end if
end tell
APPLESCRIPT}
我知道我可能忽略了一些明显的东西,但是有没有一种简单的方法我可以只用一个 Applescript 片段来做到这一点 returns 两个变量都变成 Ruby (注意,标题)?
从 AppleScript 中获取多个值是比较容易的部分。 return 不是 return 单个值,而是 return 一个列表:
result = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
if notebook of item 1 of the_selection is (notebook named "Writing") then
set title to title of item 1 of the_selection
set html to HTML content of item 1 of the_selection
return { title, html }
end if
else
return ""
end if
end tell
APPLESCRIPT}
困难的部分是解析输出。假设你的 title
是 This is a note
并且 html
是 <h1>Hello World</h1>
,osascript
将 return 这个:
This is a note, <h1>Hello World</h1>
您可以在 ,
上拆分它,但如果 title
恰好包含一个逗号,您就有问题了。您还可以将 -ss
选项传递给 osascript
,这使其成为 return 格式的 AppleScript objects,但您不想在 Ruby.
如果您知道 title
永远不会包含换行符,另一种方法是在标题后添加一个换行符:
result = %x{ osascript <<APPLESCRIPT
...
if notebook of item 1 of the_selection is (notebook named "Writing") then
set title to title of item 1 of the_selection
set html to HTML content of item 1 of the_selection
return title & "\n" & html
end if
...
APPLESCRIPT}.chomp
现在输出 (result
) 将如下所示:
This is a note
<h1>Hello World</h1>
...您可以这样获得标题和 HTML:
title, html = result.split("\n", 2)
puts title
# => This is a note
puts html
# => <h1>Hello World</h1>
现在,如果您的任何标题中有换行符(我不记得 Evernote 是否允许这样做),或者如果您想 return 超过两个值,这将是有问题的以及。下一个最简单的解决方案是选择某种不太可能出现在输出的任何部分的定界符,例如 %!%
:
DELIMITER = '%!%'
result = %x{ osascript <<APPLESCRIPT
...
if notebook of item 1 of the_selection is (notebook named "Writing") then
set title to title of item 1 of the_selection
set html to HTML content of item 1 of the_selection
return title & "#{DELIMITER}" & html
end if
...
APPLESCRIPT}.chomp
# =>This is a note%!%<h1>Hello World</h1>
title, html = result.split(DELIMITER, 2)
如果一切都失败了,您可以使用插件使 osascript
输出一种 Ruby 知道如何解析的已知格式。刚才我发现这个免费的 JSON Helper 看起来很方便。