Question: I'm trying to make an apple script to allow me to manage a cpp project automatically. And I used Jenkins too.

Basically, the cpp project is a kind of infinite loop, which will execute in a terminal. And I set Jenkins rebuild and execute it per 20 sec. In other words, my pc should do the tasks below:  

1) rebuild the project;  

2) execute the project;  

3) wait for 20 sec;  

4) close the terminal.  

However, when I arrive at 4), a dialog will pop up: Do you want to close this window?  

So I have to click the button "Close".

My question is: how to code in applescript to do the 4) automatically?  

Here is my applescript:

```
tell application "Terminal"
    do script "cd /Users/Shared/Jenkins/Home/workspace/test"
    do script "make clean" in window 1
    do script "make" in window 1
    do script "./matrix.out" in window 1
    delay 20
    close window 1  # problem's here
end tell

```

Answer: 
I don't know if you tried the other approach, but I think this is simpler and shorter:

* 1) Store the full path of the document in a string: `oldfile = ActiveDocument.FullName`
* 2) SaveAs the document with `ActiveDocument.SaveAs`
* 3) Delete the old file with `Kill oldfile`

All this is from VBA directly, no need to use Explorer shell. The same exists for the other applications.

Here is a fully working code for the Word Documents:

```
Sub RenameActiveDoc()

    Dim oldfile As String

    Set myDoc = ActiveDocument

    '1) store current file
    oldfile = myDoc.FullName

    '2) save as the active document (prompt user for file name)
    myDoc.SaveAs FileName:=InputBox("Enter new name", "Rename current document", myDoc.Name)

    '3) Delete the old file with
    On Error GoTo FileLocked
    Kill oldfile
    On Error GoTo 0

    Exit Sub

FileLocked:
    MsgBox "Could not delete " & oldfile, vbInformation + vbOKOnly, "File is locked"

End Sub

```
With contribution of [Ro Yo Mi](https://stackoverflow.com/questions/37102222/ahk-or-vba-code-to-rename-currently-opened-office-document-word-excel-powerpo/37102514#37102514) I am able to come up with following solution. However I am assuming that there might better solution to this task. 

```
 ;;; Customize Document Location (Choose form All Commands) in Quick Access Toolbar and get its position (#4 for my case)
#If WinActive("ahk_class OpusApp") || WinActive("ahk_class XLMAIN") || WinActive("PPTFrameClass")
#p:: ;Close Word/Excel/PowerPoint Document and Locate in Explorer Folder Location
clipboard = ;empty the clipboard
Send !4 ; Select full path while document location at #4 position in Quick Access toolbar
Send ^c ; copy the full path
ClipWait ; waits for the clipboard to have content
Send {esc}
Send, ^{f4} ; Close opened document only but keep Word/Excel/PPT program running
Explorer := "explorer /select," . clipboard
Run, %Explorer%\
return

```