我想創建一個可以複製克拉當前所在行的applescript。然而,在做了大量的谷歌搜索之後,我已經空了。有任何想法嗎?複製當前文本行的AppleScript?
0
A
回答
1
(*
Save this as an application
Right click the application and select "Show Package Contents"
Add this to Info.plist between <dict> and </dict>
<key>LSBackgroundOnly</key>
<true/>
launch the application with spotlight (command space)
*)
tell application "System Events"
key code 123 using {command down, shift down}
key code 124 using {command down, shift down}
keystroke "c" using {command down}
end tell
+0
謝謝,太簡單了。驚訝我沒有意識到,按鍵組合會產生我想要的結果。 Thx再次。 – maxedison 2012-07-22 15:24:00
0
下面是我爲Xcode 9寫的一些代碼(我終於得到它的更新3-5-18)。我想要一個功能,即過去25年來我使用的每個文本編輯器。沒有選擇的副本將複製當前行。代碼找出插入符號的段落(行),以及是否選擇了任何文本。相同的代碼可以修改爲包含Cut。
我使用Keyboard Maestro執行此代碼。添加下面的(3)動作。在執行它之前,必須禁用「Execute Applescript」宏,否則從代碼內發送的Command + C將導致無限循環: 禁用宏「當前宏」 執行Applescript「將AppleScript代碼粘貼到編輯框中「 啓用宏」當前宏「
另外,啓用Xcode 9鍵綁定以使Command + L執行選擇行。使用「文本」標籤查看更少的操作。
我還將以下內容添加到DefaultKeyBinding.dict文件中。 https://www.maketecheasier.com/fix-home-end-button-for-external-keyboard-mac/
{
/* Remap Home/End keys to be like Windows */
"\UF729" = "moveToBeginningOfLine:"; /* Home */
"\UF72B" = "moveToEndOfLine:"; /* End */
"$\UF729" = "moveToBeginningOfLineAndModifySelection:"; /* Shift + Home */
"$\UF72B" = "moveToEndOfLineAndModifySelection:"; /* Shift + End */
/* page up/down and move the caret like Windows*/
"\UF72C" = "pageUp:";
"\UF72D" = "pageDown:";
/* Option Home/End keys Beginning/End of Document like Mac */
"~\UF729" = "moveToBeginningOfDocument:"; /* Ctrl + Home */
"~\UF72B" = "moveToEndOfDocument:"; /* Ctrl + End */
"$~\UF729" = "moveToBeginningOfDocumentAndModifySelection:"; /* Shift + Ctrl + Home */
"$~\UF72B" = "moveToEndOfDocumentAndModifySelection:"; /* Shift + Ctrl + End */
/* Ctrl Home/End keys Beginning/End of Document like Mac */
"^\UF729" = "moveToBeginningOfDocument:"; /* Ctrl + Home */
"^\UF72B" = "moveToEndOfDocument:"; /* Ctrl + End */
"$^\UF729" = "moveToBeginningOfDocumentAndModifySelection:"; /* Shift + Ctrl + Home */
"$^\UF72B" = "moveToEndOfDocumentAndModifySelection:"; /* Shift + Ctrl + End */
}
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use application "Xcode-beta"
global appName
set appName to "Xcode-beta"
-- for some reason pressing command+C only works ever other time without this
--delay 0.1
tell application appName
activate
set theDocuments to every source document
-- exit if no source documents are found
if theDocuments is equal to {} then
-- copy command must go somewhere i.e. the user might have copied an Icon.
tell application "System Events" to keystroke "c" using {command down}
do shell script "logger -t 'AS DEBUG' " & "Applescript CopyCurrentLine - exit if no source documents are found"
return -- exit script
end if
-- Note:
-- window 1 is where source documents live
-- It sure would've been nice if window contained a reference to its source document but it doesn't
-- if window has been edited its' name ends with " — Edited" and it needs to be trimed off
set windowName to name of window 1
if windowName ends with " — Edited" then
set windowName to my trimText(windowName, " — Edited", "end")
end if
-- try to find the windows' current source document by matching window name to source document name
repeat with theDoc in theDocuments
set theDocName to name of theDoc
if theDocName is equal to windowName then
exit repeat -- found the document
else
set theDocName to "" -- didn't find the document
end if
end repeat
-- exit if the window is not a source document
if theDocName is equal to "" then
-- copy command must go somewhere i.e. the user might have copied an Icon.
tell application "System Events" to keystroke "c" using {command down}
do shell script "logger -t 'AS DEBUG' " & "Applescript CopyCurrentLine - exit if the window is not a source document"
return -- exit script
end if
--set theDoc to last source document
set docText to the text of theDoc
-- get location of selected text
set {startPos, endPos} to selected character range of theDoc
if (my isSelectedTextRangeEmpty(theDoc)) then
-- select current line
-- I set a keybinding in Xcode so Command+L would call the Command 'Select Line'
tell application "System Events" to keystroke "l" using {command down}
end if
-- copy the selection to the clipboard
set selectedText to my getSelectedText(theDoc, docText)
-- restore insertion point to original location
set selected character range of theDoc to {startPos, endPos}
set the clipboard to selectedText
end tell
on getSelectedText(theContainer, docText)
-- get the selected text
set {startPos, endPos} to selected character range of theContainer
if {endPos < startPos} then
return ""
else
return (text startPos thru endPos) in docText
end if
end getSelectedText
on isSelectedTextRangeEmpty(theContainer)
set selectedCharacterRange to selected character range of theContainer
set {startPos, endPos} to selectedCharacterRange
if {endPos < startPos} or ¬
(selectedCharacterRange is equal to {}) or ¬
(length of selectedCharacterRange is equal to 0) then
return true
else
return false
end if
end isSelectedTextRangeEmpty
on trimText(theText, theCharactersToTrim, theTrimDirection)
-- "beginning", "end", "both"
set theTrimLength to the length of the theCharactersToTrim
-- TRIM BEGINNING
if the theTrimDirection is in {"beginning", "both"} then
repeat while theText begins with the theCharactersToTrim
try
set theText to characters (theTrimLength + 1) thru -1 of theText as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
-- TRIM ENDING
if the theTrimDirection is in {"end", "both"} then
repeat while theText ends with the theCharactersToTrim
try
set theText to characters 1 thru -(theTrimLength + 1) of theText as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
return theText
end trimText
相關問題
- 1. AppleScript複製文件
- 2. Applescript - 將文件從當前文件夾複製到另一個文件夾
- 3. 複製前的HTML文本
- 4. 獲取當前文本框以進行復制/粘貼
- 5. AppleScript複製後使用的文件
- 6. 複製一個文件的AppleScript
- 7. 的AppleScript將文件複製到主盤
- 8. 將路徑複製到當前行
- 9. PeopleSoft Rowset僅複製當前行
- 10. Visual Studio 2017 - 如何複製當前行?
- 11. 多行文本框中的當前行
- 12. 當前複製網址到格式的文本框使用jQuery
- 13. 使用AppleScript將.rtf文本複製到電子郵件正文
- 14. Jenkins - 將參數從先前版本複製到當前版本
- 15. 將文件複製到當前目錄
- 16. 如何在記事本++中複製當前行?
- 17. Applescript:我不能移動複製文件
- 18. MS Word宏,將當前段落文本複製到剪貼板
- 19. 使用Ant腳本複製當前日期(今天)文件
- 20. 複製當前文件名複製到剪貼板
- 21. Powershell在複製之前檢查文件的複製腳本
- 22. 執行復雜的AppleScript中
- 23. 如何使用AppleScript將文本複製到剪貼板
- 24. 如何從AppleScript代碼複製(複製)和移動文件夾?
- 25. 將模式之前的文本複製到同一行?
- 26. AppleScript:當前應用程序的路徑
- 27. 當列匹配文本時,將整行復制到新文件
- 28. 重複的文件 - AppleScript
- 29. 複製選擇在最前面的應用程序使用AppleScript
- 30. AppleScript - 將當前URL更改爲JavaScript的腳本
如果你只是想指定快捷鍵文本編輯宏,[DefaultKeyBinding.dict(http://lri.me/keybindings.html)常常是一個好得多的選擇。 – user495470 2012-07-22 17:06:32