Skip to content

Instantly share code, notes, and snippets.

@AlickH
Last active November 6, 2021 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlickH/523c6bf2247d016cceb46e96692d1762 to your computer and use it in GitHub Desktop.
Save AlickH/523c6bf2247d016cceb46e96692d1762 to your computer and use it in GitHub Desktop.
Run Script in Coteditor
-- __setting_______________________________________________________________
-- Shows dialog if the document wasn't saved?
property showAlertDialog : true
-- __main_______________________________________________________________
-- get file path from CotEditor
tell application "CotEditor"
if not (exists front document) then return
set theFile to file of front document
end tell
-- end script if no file path is specified
if theFile is missing value then
if showAlertDialog then
display alert "No file path is specified." message "Please save the file first." as warning
return
end if
return
end if
set dirPath to do shell script "dirname " & quoted form of (theFile's POSIX path)
set fileName to do shell script "basename " & quoted form of (theFile's POSIX path)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set fileExt to last text item of (theFile's POSIX path)
set AppleScript's text item delimiters to saveTID
if fileExt ends with "." then set fileExt to text 1 thru -2 of fileExt
tell application "Finder" to set myFolder to container of (path to me) as string
set argumentFile to myFolder & "Argv.txt"
set emptyFile to get eof file argumentFile
if emptyFile is 0 then
set theResponse to ""
else
set theResponse to (read file argumentFile)
end if
-- open in terminal
tell application "Terminal"
activate
tell application "System Events"
if exists (window 1 of process "Terminal") then
tell application "System Events" to tell process "Terminal" to keystroke "w" using {command down}
end if
end tell
if fileExt = "py" then
do script "cd " & quoted form of dirPath & ";python3 " & quoted form of fileName & " " & theResponse
else if (fileExt = "scpt") or (fileExt = "applescript") then
do script "cd " & quoted form of dirPath & ";osascript " & quoted form of fileName & " " & theResponse
else if fileExt = "sh" then
do script "cd " & quoted form of dirPath & ";bash " & quoted form of fileName & " " & theResponse
else if fileExt = "rb" then
do script "cd " & quoted form of dirPath & ";ruby " & quoted form of fileName & " " & theResponse
else if fileExt = "php" then
do script "cd " & quoted form of dirPath & ";/opt/homebrew/bin/php " & quoted form of fileName & " " & theResponse
else if fileExt = "lua" then
do script "cd " & quoted form of dirPath & ";lua " & quoted form of fileName & " " & theResponse
else
do script "cd " & quoted form of dirPath
display alert "No file compiler is specified." message "Please compile yourself." as warning
end if
end tell
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
tell application "Finder"
set myFolder to container of (path to me) as string
end tell
set argumentFile to myFolder & "Argv.txt"
tell application "System Events"
if not (exists file argumentFile) then
set theOpenedFile to open for access file argumentFile with write permission
set eof of theOpenedFile to 0
write "" to theOpenedFile starting at eof
close access theOpenedFile
end if
end tell
set emptyFile to get eof file argumentFile
if emptyFile is 0 then
set defaultAnswer to ""
else
set defaultAnswer to (read file argumentFile)
end if
set theText to text returned of (display dialog "Arguments:" default answer defaultAnswer with icon note buttons {"Cancel", "Continue"} default button "Continue")
writeTextToFile(theText, argumentFile, true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment