XCode 5 - AppleScript - How to get document in current tab - xcode

I want to open a document in the current tab in an external application (MacVim for example). Based on a StackOverflow answer I created an Automator service with following AppleScript code:
tell application "Xcode"
set current_document to last source document
set current_document_path to path of current_document
end tell
tell application "MacVim"
activate
open current_document_path
end tell
The issue is, that it opens the file from the first tab and not from the current tab. How can I get the path of the current tab?

Following workaround based on SO answer works for me.
As noted in the comment there: This works EXCEPT if you are using the Assistant editor - then you end up with whatever happens to be in the Standard Editor. – Lloyd Sargent but for me it's better than the first tab.
on run {input, parameters}
set current_document_path to ""
tell application "Xcode"
set last_word_in_main_window to (word -1 of (get name of window 1))
if (last_word_in_main_window is "Edited") then
display notification "Please save the current document and try again"
-- eventually we could automatically save the document when this becomes annoying
else
set current_document to document 1 whose name ends with last_word_in_main_window
set current_document_path to path of current_document
end if
end tell
tell application "MacVim"
if (current_document_path is not "") then
activate
open current_document_path
end if
end tell
return input
end run

try the following
tell application "Xcode"
set docs to source documents
if length of docs is less than 1 then
display dialog "for ....... I need at least 1 document"
return
end if
path of last item of source documents
end tell

Related

Apple Script: how do I watch for new download file with specific name to trigger a script?

I have a script that downloads a report from an online service, waits a specified amount of time e.g. 30secs for the file to actually download and then renames the file. The script then repeats
What I would like to do is instead of putting a static delay in the script, to create a trigger that looks for the new file and once it appears triggers the renaming portion of the script.
The downloaded file name is constant.
The current portion of the script looks like this
`
tell application "Finder" to activate
tell application "System Events"
delay Wait_Time
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
ultimately what I would like is to replace the delay Wait_Time with a something like:
repeat until file name found
Search Folder for "file name"
end repeat
select new file
rename new file to clipboard
obviously this wont work but it kind of captures my logic.
I have also tried a few other possible solutions but Im new to AppleScript and not overly confident that these approaches are suitable for my application.
The below is an example of a solution I have tried. This is just an example and im not sure if this particular code can even get me to where I need to be. I thought I would include it to show just how lost I am.
set excludes to {"Folder", "Application"}
tell application "Finder"
set search_folder to folder "Macintosh HD:Users:XXXXXXXX:Library:CloudStorage:OneDrive-Flinders:Flinders Connect Stats:Source Stats:Cisco Finesse:Agent State Summary by Interval Report"
set foundItems to (every item in search_folder whose name contains "Agent State Summary by Interval Report" and kind is not in excludes) as alias list
if foundItems is {} then return
repeat with once from 1 to 100
try
if (count of foundItems) = 1 then
tell application "Finder" to activate
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
exit repeat
end if
end try
end repeat
return
end tell
Im sure you can see I have no idea what im doing.
Anyway any help would be awesome!
I have found a solution... if anyone has ideas for improvement please let me know.
tell application "Finder" to activate
tell application "System Events"
repeat until (exists (files of folder folderPath whose name contains "Agent State Summary by Interval Report"))
delay 1
end repeat
end tell
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
end repeat
say "Finished!"
display dialog "Completed"
The original solution was found here AppleScript: How to repeat a search for a file until it is found?

Open/search Spotify track in Apple Music (with Applescript)

I'd like an easy way to switch from a Spotify release to the same release in Apple Music.
I already found a way to search for the currently playing Spotify track in the Apple Music web player with Applescript:
tell application "Spotify"
if player state is not stopped then
set currentArtist to artist of current track as string
set currentTrack to name of current track as string
open location "https://music.apple.com/search?term=" & currentArtist & " " & currentTrack
end if
end tell
I'd love to:
Open the search in the native Music.app, not the web player. Is this supported?
Ideally not do a search, but go straight to the same release. Maybe with ISRC codes?
Take any selected Spotify track, not just the currently playing one. Looking at the Spotify Applescript dictionary tells me this in not possible.
had a similar problem right now and quickly hacked it out. In my case I want to simply trigger a search on the Music app.
Opened Automator and created a new "Service".
Workflow receives current > "Text" > in "every application".
Here's the AppleScript:
on run {input, parameters}
tell application "Music"
activate
end tell
tell application "System Events"
tell process "Music"
set window_name to name of front window
set value of text field 1 of UI element 1 of row 1 of outline 1 of splitter group 1 of window window_name to input
keystroke ""
key code 36
end tell
end tell
return input
end run
I saved it as "Find on Music" in Automator and now I can select text, right click > Service > Find on Music and Music plops open and shows me the results for the selected text. I hope you can use some parts of it.
I just figured out how to pass text from wherever to the search field in Music, with help from daemon's answer, which no longer works. This should work for what you want to do in conjunction with what you have.
Replace your "open location" line with a variable name for your concatenated string. Add this code below yours and pass that variable in place of 'input' (in my case 'input' is text from any application, which I use to select text of an artist name in an email/webpage/message that I want to send to Music's search).
First it checks to see if the main Music window is open vs the MiniPlayer, and open it if not to enable search via cmd-O, the cmd-F to find, then passes the input and hits return:
tell application "Music"
activate
end tell
tell application "System Events"
if not (exists (window "Music" of process "Music")) then
tell process "Music"
keystroke "0" using command down
end tell
end if
tell process "Music"
keystroke "f" using command down
keystroke input
key code 36
end tell
end tell
So, something like this (I don't have Spotify to check that section, but this should work assuming your code there is correct):
tell application "Spotify"
if player state is not stopped then
set currentArtist to artist of current track as string
set currentTrack to name of current track as string
set spotTrack to currentArtist & " " & currentTrack
end if
end tell
tell application "Music"
activate
end tell
tell application "System Events"
if not (exists (window "Music" of process "Music")) then
tell process "Music"
keystroke "0" using command down
end tell
end if
tell process "Music"
keystroke "f" using command down
keystroke spotTrack
key code 36
end tell
end tell
The only thing I couldn't figure out is how to check if the search field is already in focus, because if it is, the cmd-F causes a system alert sound. Generally not an issue as typically you'll search and interact with something else before running this script again, so calling it good. :)

How to get name of frontmost app with AppleScript and use it to get the filepath

What I try to do:
When I'm in one of my text editors (TextEdit, Byword, FoldingText) I want this AppleScript to display the file path.
I figured asking for the frontmost window app get's me the apps name nice and easily and then I can ask for the POSIX path in the next step.
The Problem:
The script is already 99% there, but I'm missing something. When I try to use the variable of activeApp it doesn't work and I get this error:
Error Number:System Events got an error: Can’t get application {"TextEdit"}.
-1728
Here's the script:
tell application "System Events"
set activeApp to name of application processes whose frontmost is true
--This doesn't work either:
--do shell script "php -r 'echo urldecode(\"" & activeApp & "\");'"
tell application activeApp
set myPath to POSIX path of (get file of front document)
end tell
display dialog myPath
end tell
If I exchange activeApp with "TextEdit" everything works. Help would be appreciated.
Maybe there's something in here that helps: Get process name from application name and vice versa, using Applescript
Either get the path property of a document or use System Events to get value of attribute "AXDocument":
try
tell application (path to frontmost application as text)
(path of document 1) as text
end tell
on error
try
tell application "System Events" to tell (process 1 where frontmost is true)
value of attribute "AXDocument" of window 1
end tell
do shell script "x=" & quoted form of result & "
x=${x/#file:\\/\\/}
x=${x/#localhost} # 10.8 and earlier
printf ${x//%/\\\\x}"
end try
end try
The first method didn't work with Preview, TextMate 2, Sublime Text, or iChm, and the second method didn't work with Acorn. The second method requires access for assistive devices to be enabled.
You are asking for...
set activeApp to name of application processes whose frontmost is true
Notice "processes", that's plural meaning you can get several processes in response so applescript gives you a list of names. Even though only one application is returned it's still in list format. Also see that your error contains {"TextEdit"}. The brackets around the name mean it's a list, so the error is showing you the problem.
You can't pass a list of names to the next line of code. As such you have a couple of choices. 1) you can ask for only 1 process instead of all processes. That will return a string instead of a list. Try this code...
set activeApp to name of first application process whose frontmost is true
2) you can work with the list by using "item 1 of the list". Try this code...
set activeApps to name of application processes whose frontmost is true
set activeApp to item 1 of activeApps
Finally, you shouldn't be telling system events to tell the application. Separate those 2 tell blocks of code. Here's how I would write your code.
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
end tell
try
tell application activeApp
set myPath to POSIX path of (get file of front document)
end tell
tell me
activate
display dialog myPath
end tell
on error theError number errorNumber
tell me
activate
display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
end tell
end try
I can't promise the "get file of front document" code will work. That depends on the application. Not all applications will understand that request. That's why I used a try block. In any case though you can be certain you are addressing the proper application. Good luck.
I've been using this snippet for a while, seems to work for all Cocoa apps (not sure about X11):
set front_app to (path to frontmost application as Unicode text)
tell application front_app
-- Your code here
end tell
None of this seems to work with a compiled AppleScript saved as an application and placed on the Dock. Whenever you run the application, IT is the frontmost, not the application that is showing its front window. That application becomes inactive as my Applescript runs. How do I write an Applescript application that isn't active when it runs?
I may have found a solution to the problem listed above. Just tell the user to reactivate the desired application, and give them time.
tell application "Finder"
activate
say "Click front window of your application"
delay 5
set myapp to get name of first application process whose frontmost is true
-- etc.
-- your code
end tell

How to open a new window and multiple urls in Safari with apple script?

How do you open a new window in safari and then open multiple tabs with different urls in that window using apple script?
The way to create a new window in Safari is to use the make new document command:
make new document at end of documents with properties {URL:the_url}
This will create a new window with a single tab pointing to the_url and make that window frontmost. Note that make new window at end of windows doesn't work, and just errors out with "AppleEvent handler fails".
Similarly, to create a new tab within a window w, you can use make new tab:
make new tab at end of tabs of w with properties {URL:the_url}
This will create a new tab in window w at the end of the list of tabs; this tab will be pointing to the_url, and it won't be the current tab. Instead of explicitly saying tabs of w, you can also use a tell w block:
tell w
make new tab at end of tabs with properties {URL:the_url}
end tell
That way, tabs implicitly refers to tabs of w.
Putting this all together, we get the following script. Given a list of URLs in the_urls, it will open all of them in a new window; if the_urls is empty, it opens a window with a blank tab.
property the_urls : {¬
"http://stackoverflow.com", ¬
"http://tex.stackexchange.com", ¬
"http://apple.stackexchange.com"}
tell application "Safari"
if the_urls = {} then
-- If you don't want to open a new window for an empty list, replace the
-- following line with just "return"
set {first_url, rest_urls} to {"", {}}
else
-- `item 1 of ...` gets the first item of a list, `rest of ...` gets
-- everything after the first item of a list. We treat the two
-- differently because the first item must be placed in a new window, but
-- everything else must be placed in a new tab.
set {first_url, rest_urls} to {item 1 of the_urls, rest of the_urls}
end if
make new document at end of documents with properties {URL:first_url}
tell window 1
repeat with the_url in rest_urls
make new tab at end of tabs with properties {URL:the_url}
end repeat
end tell
end tell
tell application "Safari"
activate
set the URL of document 1 to "http://www.XXXXXXX.com"
my new_tab()
set the URL of document 1 to "http://www.XXXXXX.com"
end tell
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
«event prcsclic» «class menI» "New Tab" of «class menE» "File" of «class mbar» 1
end tell
end tell
end new_tab
Replace the X's with whatever sites you want and keep repeating the code (my new_tab() and set the URL... lines) for each page you'd like to have open.
Referring to this page.
Correct me if this isn't what you were talking about.
Base on Pugmatt's answer I got the following to work...
on run {input, parameters}
tell application "Safari"
activate
make new document with properties {URL:"http://www.apple.com"}
my new_tab()
set the URL of document 1 to "http://www.example.com"
end tell
end run
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
«event prcsclic» «class menI» "New Tab" of «class menE» "File" of «class mbar» 1
end tell
end tell
end new_tab
I'm not sure if this is the most efficient way of you this.

How to read a file and open Safari with the read result with Mac (AppleScript)?

I need to open multiple Safari (or open the tab is OK) based on the read result.
For example, if a file has
http://a.com
http://b.com
I want to open a.com and b.com using Safari.
How can I do that with Mac/AppleScript?
Maybe I can run python calling "open -a Safari "http://a.com", but I guess AppleScript is the tool for this kind of job.
not sure about python but this will read a text file and open windows let me see if I can get tabs for you though
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
repeat with aline in locations
if length of aline is greater than 0 then
tell application "Safari"
make new document at end of documents
set URL of document 1 to aline
end tell
end if
end repeat
EDIT:
Ok this is better and it opens them in tabs of a single window
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
tell application "Safari"
activate
set adoc to make new document
end tell
repeat with aline in locations
if length of aline is greater than 0 then
tell application "Safari" to make new tab at end of window 1 with properties {URL:aline}
end if
end repeat
New Addtion
this is yet another way based on regulus6633's post in conjunction with mine
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
repeat with aLocation in locations
tell application "Safari" to open location aLocation
end repeat
If you want it to specifically open the links in Safari then mcgrailm's solution is good. However, you don't need the Finder for the first part so take that code out of the Finder tell block. There's no need to tell the Finder to do something that applescript can do itself.
However, you probably want to open the links in whatever browser is the user's default browser. It may be Safari or Firefox etc. You can do that with the "open location" command. So something like this is probably what you want...
set theFile to choose file with prompt "Pick text file containing urls"
set locations to paragraphs of (read theFile)
repeat with aLocation in locations
try
open location aLocation
end try
end repeat

Resources