Wenyan: WINDOWS環境下文言腳本自動運行方法之一

Created on 31 Jul 2020  ·  1Comment  ·  Source: wenyan-lang/wenyan

操作系統:Windows

安裝文言執行環境后,一般可以在命令行使用以下方式運行:

wenyan script.wy

若欲雙擊腳本以運行,則可將.wy文件綁定執行方式。assoc+ftype方式註冊腳本類型與指定打開程序。但這種方式還是稍顯麻煩,因爲會動到系統設置。在這個思路上再進一步簡省,不去註冊新文件類型,用已有文件打開方式可否執行文言腳本呢?答案是可行的。我們在Windows下執行cmd或者bat后綴的時候就可以直接運行,因爲它們是系統已經註冊好的可執行腳本,而其文本屬性又剛好能爲文言腳本所用,隻需要在腳本啓動后切換一下就可以了。於是可以組成以下代碼:

test.bat

@wenyan %~s0 & pause && goto :EOF
或云『爲』蓋謂『為』。
吾有一術。名之曰「執行測試」。是術曰。
    夫『天地,好在否。』。書之。
是謂「執行測試」之術也。

爲是三遍。
    施「執行測試」。
云云。

將腳本保存爲UTF-8編碼。鼠標雙擊此批處理,即可以批處理程序調用自身所包含的文言腳本。UTF-8編碼的非ASC文本,在批處理中會顯示亂碼,但是屬于批處理腳本周期的第一句恰好爲ASC文本,第一句執行完畢后即退出批處理程序,后方UTF-8內容不再加載。如此,即可使二者相互執行而互不影響。
謂之『批文混編術』XD

tips

Most helpful comment

Nice trick! Here's a version for unix systems:

wenyan "$0"; exit

或云『爲』蓋謂『為』。
吾有一術。名之曰「執行測試」。是術曰。
    夫『天地,好在否。』。書之。
是謂「執行測試」之術也。

爲是三遍。
    施「執行測試」。
云云。

and

chmod +x test.wy
./test.wy

XD

EDIT:

One might think the "shebang" line would work, which is the more "traditional" way of executing scripts in unix, e.g.

#!/usr/local/bin/wenyan

But actually it doesn't work! because the wenyan compiler itself is using the shebang trick. /usr/local/bin/wenyan is a symlink to one index.min.js which begins with the line #!/usr/bin/env node :P

One alternative is perhaps one can use pkg to package wenyan into an executable themselves, in which case the shebang line should work (haven't tested).

Therefore wenyan "$0"; exit is still the best solution :)

>All comments

Nice trick! Here's a version for unix systems:

wenyan "$0"; exit

或云『爲』蓋謂『為』。
吾有一術。名之曰「執行測試」。是術曰。
    夫『天地,好在否。』。書之。
是謂「執行測試」之術也。

爲是三遍。
    施「執行測試」。
云云。

and

chmod +x test.wy
./test.wy

XD

EDIT:

One might think the "shebang" line would work, which is the more "traditional" way of executing scripts in unix, e.g.

#!/usr/local/bin/wenyan

But actually it doesn't work! because the wenyan compiler itself is using the shebang trick. /usr/local/bin/wenyan is a symlink to one index.min.js which begins with the line #!/usr/bin/env node :P

One alternative is perhaps one can use pkg to package wenyan into an executable themselves, in which case the shebang line should work (haven't tested).

Therefore wenyan "$0"; exit is still the best solution :)

Was this page helpful?
0 / 5 - 0 ratings