Advertisement
TheCountryGamer

Run Program.app

Feb 1st, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. on open itemsOpened
  2.     repeat with itemOpened in itemsOpened
  3.         appProccess(itemOpened)
  4.     end repeat
  5. end open
  6.  
  7. -- set the path which the filePath will start at
  8. set defaultPath to (path to home folder as text) & "Programming:"
  9.  
  10. -- get the path to the file that will be run
  11. set filePath to (choose file default location defaultPath as alias) as text
  12. appProccess(filePath)
  13.  
  14. (* Main Routine *)
  15. to appProccess(fileOpened)
  16.     tell application "Terminal"
  17.         do script ""
  18.     end tell
  19.    
  20.     set filePath to (fileOpened as alias) as text
  21.    
  22.     -- split the file path for automation
  23.     set sequence to split(filePath, ":")
  24.    
  25.     -- set the full name of the file (ie "name.ending")
  26.     set fileFullName to the end of sequence
  27.     -- gets the split version of the full name (ie {name, ending})
  28.     set fileNameArray to split(fileFullName, ".")
  29.     -- gets the first index of the array (ie "name")
  30.     set fileName to (the beginning of (fileNameArray))
  31.     -- gets the last index of the array (ie "ending")
  32.     set fileExt to (the end of (fileNameArray))
  33.    
  34.     -- start to proccess the paths to the file
  35.     set terminalScriptPath to "/"
  36.     set ASScriptPath to "Macintosh HD:"
  37.    
  38.     -- delete the "Macintosh HD" and the file's name
  39.     set sequence to sequence's (items 2 thru -2)
  40.    
  41.     -- Contruct the true path using /dir/dir/dir/file
  42.     -- Recontruct the applescript path using dir:dir:dir:file
  43.     repeat with str in sequence
  44.         set terminalScriptPath to (terminalScriptPath & str & "/")
  45.         set ASScriptPath to (ASScriptPath & str & ":")
  46.     end repeat
  47.    
  48.     -- change directory command for terminal
  49.     set cd to "cd " & "\"" & terminalScriptPath & "\""
  50.    
  51.     -- continues based on type of file
  52.     if fileExt is equal to "py" then
  53.         -- this command runs the python file
  54.         set bash to "python " & fileFullName
  55.         -- run terminal with these commands, dont delte any files
  56.         terminal(cd, {bash}, "")
  57.     else if fileExt is equal to "cpp" then
  58.         -- compile and run c++ file
  59.         set bashCompile to "g++ -o " & fileName & ".out " & fileFullName
  60.         -- output the result of teh c++ file
  61.         set bashOutput to "./" & fileName & ".out"
  62.         -- run terminal with these commands, delete the output file
  63.         terminal(cd, {bashCompile, bashOutput}, ASScriptPath & fileName & ".out")
  64.     else if fileExt is equal to "java" then
  65.         -- compile the java file
  66.         set bashCompile to "javac " & fileFullName
  67.         -- run the java file from the .class file created when compiled
  68.         set bashRun to "java " & fileName
  69.         -- run terminal with these commands, delete the .class file
  70.         terminal(cd, {bashCompile, bashRun}, ASScriptPath & fileName & ".class")
  71.     else
  72.         -- log that there was no valid algorithm to run this type of file
  73.         log ("Cannot run file type; " & fileExt & return & "No valid algorithm to run this")
  74.         return
  75.     end if
  76. end appProccess
  77.  
  78.  
  79. (* Subrouties *)
  80.  
  81. -- splits the given text for decontruction of file paths and file names
  82. to split(someText, delimiter)
  83.     set AppleScript's text item delimiters to delimiter
  84.     set someText to someText's text items
  85.     set AppleScript's text item delimiters to {""} --> restore delimiters to default value
  86.     return someText
  87. end split
  88. -- 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
  89. to terminal(cdCommand, commandArray, deleteFilePath)
  90.     tell application "Terminal"
  91.         activate
  92.         do script cdCommand in front window
  93.         repeat with command in commandArray
  94.             delay 0.3
  95.             do script command in front window
  96.         end repeat
  97.     end tell
  98.     if deleteFilePath is not equal to "" then
  99.         delay 1
  100.         try
  101.             set theFilename to alias (deleteFilePath)
  102.             tell application "Finder" to delete file (theFilename as text)
  103.         end try
  104.     end if
  105. end terminal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement