How to a file uploading using Automator in apple script? - macos

I’m new to automator. This is what I’m trying to achieve.
I have the code for uploading the file in Safari using applescript ( which will upload the file myFile points to):
on run argv
set myFile to item 1 of argv
activate application "Safari"
tell application "System Events" to tell process "Safari"
keystroke "G" using {command down, shift down}
delay 2
key code 51
delay 2
keystroke myFile
delay 30
key code 52
delay 10
key code 52
delay 5
end tell
end run
This applescript is been called by Java and it works fine.
This is working totally fine. But I want to put this in automator service, so that Safari has directly access to the applescripts, rather than Java doing it. Now I have two questions:
Many Safari will be running and calling this service. So which safari needs to give the file uploading process needs to be figured out. Is it possible for the automator to find the process id of the application which called it ?
If so, then how shall I pass the parameter (the file location to be uploaded) to the automator, when the service is called?
Thanks in advance.

Related

AppleScript works in Script Editor but not as application

I am pretty new to programming, especially with AppleScript. I wrote a simple script for Valentine's Day to play a song from iTunes and then open a flash animation file in Safari. When I run the script in ScriptEditor, everything works as desired, but when I export as a standalone application, it fails at the command to enable full-screen mode. I am assuming it is an issue with System Events. To be clear, the application functions to the end, but at the keystroke command I hear an alert sound and the window remains as-is.
I am running Yosemite, and am fully updated.
Ideally, I would like to open the file in Google Chrome to utilize Presentation Mode, but I can't even get Chrome to open the file.
Thanks for any advice! Here is the code:
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
close every window
end tell
set volume output volume 75
tell application "iTunes"
set currentVolume to sound volume
if player state is playing then
stop
back track
end if
play track "The Promise"
set player position to 6
end tell
delay 4
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
end tell
I agree with Jerry Stratton's comment that it could be an accessibility issue. However it also could be that you are issuing the keystroke command before Safari is ready to accept it. If it's opening a file then it could be busy and miss the keystroke command.
Also, I would move the system events code outside the Safari code and also just tell system events, rather than the Safari process, to perform the keystroke command. Try this as the Safari and System Events parts.
NOTE: I can't get Chrome to open a file either.
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
end tell
tell application "Safari" to activate
delay 1
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
Most likely you’ll need to allow your standalone application to use System Events. At some point you needed to do that for Script Editor; you’ll need to do the same for your standalone app.
You’ll find the option in System Preferences under Security & Privacy, then Privacy, and then Accessibility. There’ll be a list of apps, and your app is probably listed there without a check for “Allow the apps below to control your computer.”
You may need to use the “+” button to add your app to the list.
I have verified that I can use this simple script to make Safari full-screen; it will work if the app is given permission under Accessibility, and it will silently fail if not.
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
This is Yosemite, Mac OS X 10.10; it may be different in other versions of Mac OS X.

How can I trigger keyboard commands via AppleScript in a Flash-generated upload prompt?

I'm trying to submit upload a photo to a website via AppleScript.
The basic experience is:
Load Site [done]
Click "Upload" [done - triggers Choose File prompt in what looks like the Finder, but still says Safari in top left]
Type file path in prompt and submit using:
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke "/Users/myUser/Pictures/myPic.jpg"
key code 36
end tell
But instead the AppleScript just stops moving once the prompt appears, and waits for human interaction. How can I use AppleScript to upload an image in a prompt like this?
Demo of the web side of things: http://www.uploadify.com/demos/
Assume I can click the "Select Files" button in this demo via AppleScript. How can I continue to use AppleScript to enter the file path and submit?
You need delays in the code like #mklement0 said
and to also make sure safari is activated
tell application "safari"
activate
tell application "System Events"
keystroke "g" using {shift down, command down}
delay 1.6
keystroke "/Users/myUser/Pictures/myPic.jpg"
delay 1.6
key code 36
end tell
end tell

AppleScript: How to reload a Safari window without bring the window to the foreground while I am in other Desktop space

I want to write an AppleScript to achieve a simple task. That is, I open a Safari window on Desktop 1 and automatically reload the browser every 30 minutes while I do my daily work on Desktop 2.
Here is my script:
tell application "Safari"
activate
tell application "System Events"
tell process "Safari"
keystroke "r" using {command down}
end tell
end tell
end tell
This script works. However, whenever the script is executed, my current Desktop 2 will be brought back to Desktop 1. It is distracting to my workflow.
Is there any way to just let Safari to reload in the background without bring Safari window to foreground on Desktop 1?
I have done a couple of searches; many of them say I should not use "activate" in my script. I tried that but then the script will just do nothing.
You can simply get the URL of document 1, then tell Safari to set the URL of document 1 to that URL.
The effect is the page will reload. Without bringing Safari to the front or jumping to Safari while you are in another space.
tell application "Safari"
set docUrl to URL of document 1
set URL of document 1 to docUrl
end tell
Also a simple on idle handler and saved as an Application (Stay open checked) that will run every 1 minute but not when Safari is fronmost. i.e when you are actually using it.
on idle
set frontApp to name of application (path to frontmost application as text)
if frontApp is not equal to "Safari" then
tell application "Safari"
set docUrl to URL of document 1
set URL of document 1 to docUrl
end tell
end if
return 60
end idle

In applescript, is there any way to specify which desktop a file is opened in?

When my system boots I would like to run applescript that opens files in three different desktops/spaces.
First Space: Mail and Things (my to do list program)
Second Space: Textmate and a Safari for my first project
Third Space: Textmate and a Safari for my second project
First, in Mission Control I created two more desktops which will remain there the next time my system boots unless they are manually removed. Instead of creating one long script, I chained three applescripts (boot1, boot2 and boot3) to break it up into simpler blocks of code. At the end of boot1 you will see:
run script file "<drive name>:Users:<username>:boot2.scpt"
In boot2 and boot3 you will see a bunch of delay lines. One thing I dislike about applescript is that it often starts processing the next command before the OS finishes responding to the prior one. This causes inconsistencies and errors. Delays are a hack to force things to slow down. They help, but even when you use them things are still a bit dicey. In boot2.script:
# this emulates the keyboard shortcut to move to desktop 2
# there doesn't seem to be any way to modify an `open` command to open a file on desktop 2
tell application "System Events"
delay 2
# key code 19 is the key code for the number 2.
# <cntl> 2 is the shortcut to get to desktop 2
key code 19 using control down
end tell
tell application "TextMate"
activate
# 'sites' is the name of the directory my projects are in
open "/users/<username>/sites/project1/"
end tell
tell application "Terminal"
activate
do script "cd /users/<username>/sites/project1/"
delay 2
do script "rails s" in front window
delay 2
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
tell application "System Events" to tell process "Terminal" to keystroke return
delay 2
do shell script "open -a Safari http://localhost:3000"
end tell
OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough. Boot3.script is almost the same as boot2 but when trying to open an application on desktop 3, because there is a window on desktop 2 the system jumps back to that desktop. This is the next problem. How do I overcome that?
2305491 is no longer relevant because space preferences are gone.
Thanks.
Boot3.script is almost the same as boot2 but when trying to open an application on desktop 3, because there is a window on desktop 2 the system jumps back to that desktop.
There is an option in the Mission Control Preferences called "When switching to an application, switch to a Space with open windows for the application". Uncheck this.
OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough.
Better solution is always something like this
repeat until something exists
delay 0.1
end repeat

Applescript; opening an app in Space number N

I wonder if it is possible in applescript to create a script for which we give as input the application name and a number N, so this app gets opened in the Space's space number N.
I would like with this to create a meta-applescript, so when the computer boots and after login, on each space I get different apps, and important, I can change this in the script file, and not through mac os x Space's preferences
Thanks
In OS X 10.5 or 10.6, Spaces assignments can be accessed and changed via the scriptable interface to System Events.app:
tell application "System Events"
set x to application bindings of spaces preferences of expose preferences
set x to {|com.apple.textedit|:4} & x -- Have TextEdit appear in space 4
set application bindings of spaces preferences of expose preferences to x
end tell
If you don't already know it, you can get the bundle id of an application from the Finder:
tell application "Finder"
get id of application file "TextEdit" of folder "Applications" of startup disk
end tell
This works to switch to Space 2 and then back to Space 1:
tell application "System Events"
key code 19 using {control down} -- control+2 is switch to Display Space 2
end tell
delay 1.0
tell application "System Events"
key code 18 using {control down} -- control+1 is switch to Display Space 1
end tell
delay 1.0
Answer
Although it's useful to assign applications to workspaces this doesn't address the question properly. Because, for example, you may want to launch multiple chrome windows in different spaces hence the application-to-space binding wouldn't work.
I found a workaround made of two steps to make this happen.
Change space location
tell application "System Events"
# comment: 18 to 21 somehow refer to workspaces 1 to 4, therefore here we are going to space number 1
tell application "System Events" to key code 18 using {control down}
end tell
delay 1 # comment: add some delay before launching app. this is 1 second delay
Launch the application that you want: either through another applescript or by using Launch Application
repeat the process to go to another space, and launch another app.
some notes:
Unfortunately, I have not found the corresponding key code to place an app on space number 5, if you do please let me know.
Also, this only works on the assumption that you already have the 4 spaces available (otherwise it will open things in the same space).
If things start to chain with previous scripts output and not work properly, remember to tick on each script/task of the automation Options > Ignore this action's input.
As personal observation once or twice when the computer seems busy to do something else, the prioritization of the automation seems low and the delay may be a few seconds longer.
Full example (with 4 apple scripts)
The following opens 2 google chrome windows in 2 different spaces. Code is below for copy and paste.
Script 1
tell application "System Events"
tell application "System Events" to key code 18 using {control down}
end tell
delay 1
Script 2
tell application "Google Chrome"
make new window
open location "https://www.google.com"
open location "https://www.apple.com"
end tell
delay 1
Script 3
tell application "System Events"
tell application "System Events" to key code 19 using {control down}
end tell
delay 1
Script 4
tell application "Google Chrome"
make new window
open location "https://www.bbc.co.uk"
end tell
delay 1

Resources