Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- on open itemsOpened
- repeat with itemOpened in itemsOpened
- appProccess(itemOpened)
- end repeat
- end open
- -- set the path which the filePath will start at
- set defaultPath to (path to home folder as text) & "Programming:"
- -- get the path to the file that will be run
- set filePath to (choose file default location defaultPath as alias) as text
- appProccess(filePath)
- (* Main Routine *)
- to appProccess(fileOpened)
- tell application "Terminal"
- do script ""
- end tell
- set filePath to (fileOpened as alias) as text
- -- split the file path for automation
- set sequence to split(filePath, ":")
- -- set the full name of the file (ie "name.ending")
- set fileFullName to the end of sequence
- -- gets the split version of the full name (ie {name, ending})
- set fileNameArray to split(fileFullName, ".")
- -- gets the first index of the array (ie "name")
- set fileName to (the beginning of (fileNameArray))
- -- gets the last index of the array (ie "ending")
- set fileExt to (the end of (fileNameArray))
- -- start to proccess the paths to the file
- set terminalScriptPath to "/"
- set ASScriptPath to "Macintosh HD:"
- -- delete the "Macintosh HD" and the file's name
- set sequence to sequence's (items 2 thru -2)
- -- Contruct the true path using /dir/dir/dir/file
- -- Recontruct the applescript path using dir:dir:dir:file
- repeat with str in sequence
- set terminalScriptPath to (terminalScriptPath & str & "/")
- set ASScriptPath to (ASScriptPath & str & ":")
- end repeat
- -- change directory command for terminal
- set cd to "cd " & "\"" & terminalScriptPath & "\""
- -- continues based on type of file
- if fileExt is equal to "py" then
- -- this command runs the python file
- set bash to "python " & fileFullName
- -- run terminal with these commands, dont delte any files
- terminal(cd, {bash}, "")
- else if fileExt is equal to "cpp" then
- -- compile and run c++ file
- set bashCompile to "g++ -o " & fileName & ".out " & fileFullName
- -- output the result of teh c++ file
- set bashOutput to "./" & fileName & ".out"
- -- run terminal with these commands, delete the output file
- terminal(cd, {bashCompile, bashOutput}, ASScriptPath & fileName & ".out")
- else if fileExt is equal to "java" then
- -- compile the java file
- set bashCompile to "javac " & fileFullName
- -- run the java file from the .class file created when compiled
- set bashRun to "java " & fileName
- -- run terminal with these commands, delete the .class file
- terminal(cd, {bashCompile, bashRun}, ASScriptPath & fileName & ".class")
- else
- -- log that there was no valid algorithm to run this type of file
- log ("Cannot run file type; " & fileExt & return & "No valid algorithm to run this")
- return
- end if
- end appProccess
- (* Subrouties *)
- -- splits the given text for decontruction of file paths and file names
- to split(someText, delimiter)
- set AppleScript's text item delimiters to delimiter
- set someText to someText's text items
- set AppleScript's text item delimiters to {""} --> restore delimiters to default value
- return someText
- end split
- -- run the change directory command and then run teh rest of the commands in the same window. Also if the deleteFilePath is not empty, try to delete the file it leads to
- to terminal(cdCommand, commandArray, deleteFilePath)
- tell application "Terminal"
- activate
- do script cdCommand in front window
- repeat with command in commandArray
- delay 0.3
- do script command in front window
- end repeat
- end tell
- if deleteFilePath is not equal to "" then
- delay 1
- try
- set theFilename to alias (deleteFilePath)
- tell application "Finder" to delete file (theFilename as text)
- end try
- end if
- end terminal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement