1. 基礎知識
1.1 注冊 Register
注: 本系列不包括lua基礎, 您需要掌味訓礎的Aegisub模板撰寫能力以及掌味訓礎的Lua知識后進行學習
也就是, 您至少需要掌握Aegisub手冊中的karaskel.lua條目下里的變數是如何使用的, 如line.actorline.widthline.leftline.duration等; 以及了解Lua的變數宣告, 流程控制, 函式定義這些基礎內容
1.1.1 注冊
aegisub.register_macro(name, description, main_function[, checked_fucntion])
將這行代碼敲在一個lua檔案中, 放在 [aegisub\automation\autoload] 目錄下就注冊一個自動化插件.
1.1.2 引數解釋
name: 腳本名, 在aegisub的自動化顯示的名稱description: 腳本簡介, 將滑鼠浮動在自動化上就會在左下角輸出這個簡介, 通常用來對腳本的用處進行簡單的介紹.main_function: 運行這個腳本后會呼叫的主函式, 手冊上講這個函式必須是一個宏處理函式(macro processing function), 這個下一節會進行解釋.check_function: 很少用到的一個可選引數, 你只可以在 kara-templater.lua 這個檔案, 也就是卡拉ok模板執行器主體腳本中看到它使用了這個引數. 這個引數的作用是進行一個邏輯判斷判斷腳本是否運行: 當回傳結果是false時, 這個腳本將不會被執行.
1.1.3 注冊拓展
#例1.1.3-1: kara-templater的注冊模塊
local tr = aegisub.gettext
script_name = tr"Karaoke Templater"
script_description = tr"Macro and export filter to apply karaoke effects using the template language"
script_author = "Niels Martin Hansen"
script_version = "2.1.7"
--------------------------------------------
--[[
<主函式體>
--]]
--------------------------------------------
aegisub.register_macro(tr"Apply karaoke template", tr"Applies karaoke effects from templates", macro_apply_templates, macro_can_template)
通常來說, 像例1.1.3-1一樣的注冊模塊才是我們寫的注冊模塊, 開頭注明腳本資訊, 結尾進行注冊. 當然, kara-templater不是很具體, 我們再看另一個例:
#例1.1.3-2: strip-tags的主體模塊
local tr = aegisub.gettext
script_name = tr("Strip tags")
script_description = tr("Remove all override tags from selected lines")
script_author = "Thomas Goyne"
script_version = "1.0.0"
--[[ 主函式體
function strip_tags(subs, sel)
for _, i in ipairs(sel) do
local line = subs[i]
line.text = line.text:gsub("{[^}]+}", "")
subs[i] = line
end
aegisub.set_undo_point(tr"strip tags")
end
--]]
aegisub.register_macro(script_name, script_description, strip_tags)
例1.1.3-2-[strip-tags.lua]同樣是aegisub內置的一個腳本, 我把主函式體注釋起來的, 和例1.3.1是同樣的結構.
1.1.3.1 strip_tags注冊模塊引數解釋
aegisub.gettext
aegisub.gettext是aegisub內置的一個翻譯api函式, 雖然一般我們寫腳本也會加上這個, 但是這個函式是沒用的, 因為它只會呼叫aegisub內置詞典中的詞進行本地化翻譯, 屬于可寫可不寫的引數.
#例1.1.3-3: aegisub.gettext的輸出
aegisub.gettext("Strip tags")
--> Strip tags - 洗掉特效標簽
aegisub.gettext("Remove all override tags from selected lines")
--> 從所選行中移除所有特效標簽
aegisub.gettext("Delete tags")
--> Delete tags
如例1.1.3-3, 前面兩個字串是aegisub內置的腳本所以內置了詞典會輸出中文翻譯, 而后面的"Delete tags"不屬于里面的內容所以只會輸出原來的值.
腳本資訊
script_name: 腳本名script_description: 腳本簡介script_author: 腳本作者script_version: 腳本當前版本
這4個腳本資訊寫在lua檔案的最開頭, 方便后來檢查和修改. 當然, 這4個就是最簡單的變數宣告而已, 沒有別的其它特殊含義, 如果想要添加比如腳本日期script_date什么的也都可以.
注冊函式
回到了最開頭的aegisub.register_macro()函式, 從上例中可以看出, 可以使用script_name和script_description進行腳本名和簡介的呼叫(例1.1.3-2), 也可以像kara-templater一樣直接使用字串輸入(例1.3-1).
1.1.3.2 在Aegisub中呼叫aegisub api
template line, !_G.aegisub.gettext("Strip tags")!
在Aegisub中使用函式需要添加上全域變數_G才能呼叫.
1.1.4 script_name拓展
#例1.1.4-1: 通過檔案樹路徑方式呼叫多個函式#1
script_name = "Save lines"
aegisub.register_macro(script_name.."/save", script_description, save_lines)
aegisub.register_macro(script_name.."/load", script_description, load_lines)

我們可以向script_name后面和類似檔案路徑一樣添加斜杠, 這樣呼叫自動化的時候就會有子選項出現. 因為是變數連接字串, 所以我們需要字串連接符 .. 進行連接, 例1.1.4-1等同于例1.1.4-2:
#例1.1.4-2: 通過檔案樹路徑方式呼叫多個函式#2
aegisub.register_macro("Save lines/save", script_description, save_lines)
aegisub.register_macro("Save lines/load", script_description, load_lines)
1.5 思考
- 在Aegisub中使用
aegisub.gettext()函式, 查看"Karaoke Templater"和"Apply karaoke template"會輸出什么. - 雖然現在還無法輸出效果, 但是我們已經能在Aegisub的自動化注冊一個腳本了, 嘗試一下. (在autoload目錄下新建一個lua檔案, 進行注冊, 查看是否能夠在Aegisub中找到您注冊的這個腳本.)見例1.1.3-2-[strip-tags.lua], 這就是一個完整的腳本.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/523172.html
標籤:其他
上一篇:Illustrator 2023 for mac/win(ai2023)中文
下一篇:ensp 鏈路聚合
