ScriptDocument

非推奨を表示

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

作成できません
複製されていません

A ScriptDocument インスタンスは、Studio スクリプトエディタのドキュメントのプロキシです。エディタで開いたドキュメントの一時的な状態を表し、その表現はコードの実行よりも読み込みと編集に適した形式であるため、LuaSourceContainer とは異なり、開いたドキュメントの一時的な状態を表すものであり、その表現は実行するよりも読み込みと編集に適した形式です。特に、ScriptDocument は、ソースプロパティがないドラフトモードのオープンスクリプトに行われた変更を反映します。

スクリプトエディタ自体は存在し、どの DataModel よりも異なるスレッドで変更されているので、ScriptDocument は開いたスクリプトエディタをレプリケートしますが、開いたエディタではありません。レプリケーションのため、編集者でテキストを変更して ScriptDocument を更新すると、時折遅れが生じます。遅延は、DataModel が忙しいために発生し、ほぼ常に極めて小さいが、まだ存在します。

ScriptDocument の存在は、ドキュメントがスクリプトエディタで開いていることを示します。すべての ScriptDocument インスタンスには ScriptEditorService が親として存在します。各インスタンスは次のエンコード規約に従います:

  • ScriptDocument 内のすべてのテキストは、UTF-8 でエンコードされています。
  • すべてのラインインデックスは 1 インデックスです。
  • すべてのキャラクターインデックスは 1 インデックスで、グラフェムではなく UTF-8 バイトをカウントするので、TextBox.CursorPosition から同じ警告が適用されます:多くのユニコードキャラクターは 1 バイト以上を取ります。
  • すべての範囲には、始まりの位置と終わりの位置が含まれており、始まり == 終わりは空の範囲を意味します。

すべての API は ScriptDocumentプラグイン レベルのセキュリティにあります。

概要

方法

  • GetLine(lineIndex : number?):string
    プラグインのセキュリティ

    指定された行のテキストを返します。引数が提供されないと、現在のカーソルポジションの行を返します。

  • プラグインのセキュリティ

    文書にある行数を返します。

  • プラグインのセキュリティ

    基本の LuaSourceContainer インスタンスを返し、存在する場合は、そうでない場合は nil

  • プラグインのセキュリティ

    エディタで選択されたテキスト、または選択がない場合は空の文字列を取得します。

  • プラグインのセキュリティ

    スクリプトエディタの最後に知られた選択をフォーマットで返します: CursorLine, CursorChar, AnchorLine, AnchorChar 。スクリプトエディタに選択がない場合は、CursorLine == AnchorLineCursorChar == AnchorChar

  • プラグインのセキュリティ

    カーソルポジションとアンカーのうちの大きいものを取得します。エディタに選択がない場合、両方が同じ値です。

  • プラグインのセキュリティ

    カーソルポジションとアンカーのうち、より小さいものを取得します。エディタに選択がない場合、両方が同じ値です。

  • GetText(startLine : number?,startCharacter : number?,endLine : number?,endCharacter : number?):string
    プラグインのセキュリティ

    開いたエディタからテキストを返します。

  • プラグインのセキュリティ

    エディタの変更で現在表示されている行番号を返します。

  • プラグインのセキュリティ

    エディタにテキストが選択されているかどうかを返します。

  • プラグインのセキュリティ

    ScriptDocument がコマンドバーを表示する場合、真を返します。

  • イールド
    プラグインのセキュリティ

    この文書に関連するエディタが閉じるリクエスト。エディタがリクエストに応答するまで、現在のスレッドを返します。

  • EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
    イールド
    プラグインのセキュリティ

    指定された範囲内のテキスト ( , ) を新しいテキストで置き換えます。

  • ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    イールド
    プラグインのセキュリティ

    エディタにカーソル選択を引数値に設定するように求めます。

  • イールド
    プラグインのセキュリティ
  • RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    イールド
    プラグインのセキュリティ

    エディタにカーソル選択を引数値に設定するように求めます。

イベント

プロパティ

方法

GetLine

プラグインのセキュリティ

指定された行のテキストを返します。引数が提供されないと、現在のカーソルポジションの行を返します。

パラメータ

lineIndex: number
既定値: "nil"

戻り値

コードサンプル

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Selected line text: {lineText}`)
end)
else
print("No scripts open")
end

GetLineCount

プラグインのセキュリティ

アクティブなドキュメントの行数を返します。


戻り値

コードサンプル

ScriptDocument:GetLineCount()

ScriptDocument:GetLineCount()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local lineCount = scriptDocument:GetLineCount()
print(`The script has {lineCount} lines!`)
else
print("No scripts open")
end
プラグインのセキュリティ

基本の LuaSourceContainer インスタンスを返し、存在する場合は、そうでない場合は nil


戻り値

コードサンプル

ScriptDocument:GetScript()

ScriptDocument:GetScript()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local openScript = scriptDocument:GetScript()
print(`Currently open script: {openScript:GetFullName()}`)
else
print("No scripts open")
end

GetSelectedText

プラグインのセキュリティ

エディタで選択されたテキスト、または選択がない場合は空の文字列を取得します。


戻り値

コードサンプル

ScriptDocument:HasSelectedText() and :GetSelectedText()

ScriptDocument:HasSelectedText() and :GetSelectedText()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`Currently selected text: {selectedText}`)
else
print("No text currently selected")
end
end)
else
print("No scripts open")
end

GetSelection

プラグインのセキュリティ

スクリプトエディタの最後に知られた選択をフォーマットで返します: CursorLine, CursorChar, AnchorLine, AnchorChar 。スクリプトエディタに選択がない場合は、CursorLine == AnchorLineCursorChar == AnchorChar


戻り値

CursorLine、CursorChar、AnchorLine、AnchorChar。

GetSelectionEnd

プラグインのセキュリティ

カーソルポジションとアンカーのうちの大きいものを取得します。エディタに選択がない場合、両方が同じ値です。


戻り値

コードサンプル

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`Selection start: Line {startLine}, Char {startCharacter}`)
print(`Selection end: Line {endLine}, Char {endCharacter}`)
else
print("No scripts open")
end

GetSelectionStart

プラグインのセキュリティ

カーソルポジションとアンカーのうち、より小さいものを取得します。エディタに選択がない場合、両方が同じ値です。


戻り値

コードサンプル

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`Selection start: Line {startLine}, Char {startCharacter}`)
print(`Selection end: Line {endLine}, Char {endCharacter}`)
else
print("No scripts open")
end

GetText

プラグインのセキュリティ

開いたエディタからテキストを返します。0、2、または 4 の引数で呼び出さなければなりません:

  • 0 引数で呼び出されると、開いたエディタの全体のコンテンツを取得します。
  • 2つの引数で呼ばれた場合、文書のテキストを ( startLine )、( startColumn ) から開始して取得します。
  • 4つの引数で呼ばれた場合、( )から始まる文書のテキストを取得し、( > )で終了します。

パラメータ

startLine: number
既定値: "nil"
startCharacter: number
既定値: "nil"
endLine: number
既定値: "nil"
endCharacter: number
既定値: "nil"

戻り値

コードサンプル

ScriptDocument:GetText()

ScriptDocument:GetText()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local text = scriptDocument:GetText()
print(`Script contents: {text}`)
else
print("No scripts open")
end

GetViewport

プラグインのセキュリティ

エディタの変更で現在表示されている行番号を返します。エディタは、スタートラインと終わりのラインの間の行を表示します。含まれます。最初と最後の行は部分的にしか表示されない可能性があります。たとえば、最後の行の最上のピクセルだけが画面に表示されるかもしれません。さらに、コードフォールドは startLine と endLine の間の行を隠す可能性があります。


戻り値

コードサンプル

ScriptDocument:GetViewport

ScriptDocument:GetViewport

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local firstLine, lastLine = scriptDocument:GetViewport()
print(`Currently viewing lines {firstLine} to {lastLine}`)
else
print("No scripts open")
end

HasSelectedText

プラグインのセキュリティ

エディタにテキストが選択されているかどうかを返します。


戻り値

コードサンプル

ScriptDocument:HasSelectedText() and :GetSelectedText()

ScriptDocument:HasSelectedText() and :GetSelectedText()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`Currently selected text: {selectedText}`)
else
print("No text currently selected")
end
end)
else
print("No scripts open")
end

IsCommandBar

プラグインのセキュリティ

ScriptDocument がコマンドバーを表示する場合、真を返します。コマンドバーには、この API で特別なルールと制限があります:

  • スタジオは、プラグインを実行する前にコマンドバーを作成し、データモデル間でスタジオが移行するたびに開いたイベントを必ず発射するわけではありませんが、閉じて再開します。
  • セキュリティ上の理由により、コマンドバーを EditTextAsync で編集できません。

戻り値

コードサンプル

ScriptDocument:IsCommandBar()

ScriptDocument:IsCommandBar()

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if document:IsCommandBar() then
print("Command bar document:", document)
end
end

CloseAsync

イールド
プラグインのセキュリティ

この文書に関連するエディタが閉じるリクエスト。エディタがリクエストに応答するまで、現在のスレッドを返します。機能が成功すると、返されます (true、nil)。機能が失敗すると、問題の説明として (false、strin文字列) を返します。

この機能はコマンドバーを閉じることができません。


戻り値

コードサンプル

ScriptDocument:CloseAsync

ScriptDocument:CloseAsync

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- Find the first open script document
for _, document in documents do
-- The Command Bar can't be closed, so don't select it
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`Closed {scriptDocument.Name}`)
else
warn(`Failed to close {scriptDocument.Name} because: {err}`)
end
else
print("No open scripts")
end

EditTextAsync

イールド
プラグインのセキュリティ

指定された範囲内のテキストを ( startLine , startColumn ) から ( endLine , endColumn ) に newText で置換します。範囲が空である場合、関数はテキストを挿入します(startLinestartColumn)。テキストカーソルが指定された範囲内にある場合、カーソルは編集の終了位置に移動します。そうでなければ、テキストカーソルは移動しません。この関数は、編集に関する返答を受け取るまで、現在のスレッドを返します。

機能が成功すると、返す ( true , nil )。

機能はエラーをスローする場合があります::

  • 範囲は無効です。
  • 範囲は、例えば、ユニコード文字のバイトの一部のみを置換するなど、ユニコード文字を切り取ります。
  • The newText 自体には無効な UTF-8 が含まれています。

機能が失敗すると、返されます (false、string)。文字列は問題の説明です。最も一般的な失敗タイプはバージョンの不一致です。これは、 がエディタのコンテンツとシンクロしない時間に呼び出そうとするときに発生します。このような場合は、編集を再試行できます。

パラメータ

newText: string
既定値: ""
startLine: number
既定値: ""
startCharacter: number
既定値: ""
endLine: number
既定値: ""
endCharacter: number
既定値: ""

戻り値

ForceSetSelectionAsync

イールド
プラグインのセキュリティ

エディタにカーソル選択を引数値に設定するように求めます。両方のアンカー引数をパスするか、どちらもパスしないでください。どちらもパスされない場合、それぞれ相応するカーソル引数と同じになるようにデフォルトで設定されます。文書のテキストコンテンツが変更された場合、エディタはカーソルを更新しない可能性があります。ScriptDocument:RequestSetSelectionAsync() とは異なり、編集者はリクエストが行われて以来、カーソルが移動した場合でもカーソルを移動しないようにします。カーソルが更新された場合は (true, nil)、更新されなかった場合は (false, string) 説明文字列を返します。エディタが返信するまで、現在のスレッドを返します。

パラメータ

cursorLine: number
既定値: ""
cursorCharacter: number
既定値: ""
anchorLine: number
既定値: "nil"
anchorCharacter: number
既定値: "nil"

戻り値

コードサンプル

ScriptDocument:ForceSetSelectionAsync()

ScriptDocument:ForceSetSelectionAsync()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- Get the text on the cursor's current line
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Force select the entire line of text
local success, err = scriptDocument:ForceSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Set selection!")
else
print(`Failed to set selection because: {err}`)
end
else
print("No scripts open")
end

MultiEditTextAsync

イールド
プラグインのセキュリティ

パラメータ

edits: Array
既定値: ""

戻り値

RequestSetSelectionAsync

イールド
プラグインのセキュリティ

エディタにカーソル選択を引数値に設定するように求めます。両方のアンカー引数をパスするか、どちらもパスしないでください。どちらもパスされない場合、それぞれ相応するカーソル引数と同じになるようにデフォルトで設定されます。文書のテキストコンテンツが変更されたか、リクエストが行われて以来、カーソルが移動した場合、エディタはカーソルを更新しない可能性があります。カーソルが更新された場合は (true, nil)、更新されなかった場合は (false, string) 説明文字列を返します。エディタが返信するまで、現在のスレッドを返します。

パラメータ

cursorLine: number
既定値: ""
cursorCharacter: number
既定値: ""
anchorLine: number
既定値: "nil"
anchorCharacter: number
既定値: "nil"

戻り値

コードサンプル

ScriptDocument:RequestSetSelectionAsync()

ScriptDocument:RequestSetSelectionAsync()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- Get the text on the cursor's current line
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Force select the entire line of text
local success, err = scriptDocument:RequestSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Set selection!")
else
print(`Failed to set selection because: {err}`)
end
else
print("No scripts open")
end

イベント

SelectionChanged

プラグインのセキュリティ

テキストの変更の直後を含め、スクリプトドキュメントが変更されると発火します。

パラメータ

positionLine: number
positionCharacter: number
anchorLine: number
anchorCharacter: number

コードサンプル

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Selected line text: {lineText}`)
end)
else
print("No scripts open")
end

ViewportChanged

プラグインのセキュリティ

エディタで表示される行番号が変更されると、発火します。詳細は ScriptDocument.GetViewport を参照してください。

パラメータ

startLine: number
endLine: number

コードサンプル

Demonstrates using ScriptDocument.ViewportChanged to print the start and end line of the script's viewport when it changes.

To run:

  1. Ensure Output view is open
  2. Run the below code in the Command Bar
  3. Scroll up and down in the opened Script window
Connecting to ScriptDocument.ViewportChanged

--!nocheck
--[[
To run:
1. Ensure Output view is open
2. Run the below code in the Command Bar
3. Scroll up and down in the opened Script window
Print statements from the ViewportChanged event will appear in the Output
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Create text that spans many lines
local dummyText = string.rep("-- Dummy Text\n", 60)
-- Create a script containing the dummy text and open it
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`Failed to open script because: {err}`)
return
end
-- Get a reference to the opened script
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`Script Viewport Changed - startLine: {startLine}, endLine: {endLine}`)
end
-- Connect the ViewportChanged event to the function above that prints the start and end line of the updated viewport
scriptDocument.ViewportChanged:Connect(onViewportChanged)