Final Cut Pro X with Apple script (export projects) - applescript

Trying to make a script with whom will launch FCPX and will export one project after another (per day need to export 24 videos). So far were able to make this script:
tell application "Finder"
open ("/Volumes/UpNext/Final Cut Projects/1-Monday/CurrentVersion.fcpproject" as POSIX file)
end tell
delay 14
tell application "System Events"
tell process "Final Cut Pro"
click menu item "Snapping" of menu "View" of menu bar 1
click
end tell
end tell
Instead of turning on snapping I want to do following: Click in FCPX on /File/Share/Some format.
Problem is that don't know how to launch "share" and "needed format", because "Share" is an pop up menu. My apologies for simple questions, unfortunately never in my life worked with scripts and project needs to be done ASAP. Maybe there is other way with scripts to achieve this goal (export many projects). Will be open to suggestions!

I think the best (maybe easiest) way to do this (at least to get you started) is to set up key commands in System Preferences, and to use the following method to trigger the key command.
So for example, I just used System Prefs (Keyboard>Keyboard Shortcuts>Application Shortcuts) to make a Shift-F1 key command to trigger the "Vimeo..." menu item. Now I can use cliclick to trigger that. Cliclick (http://www.bluem.net/en/mac/cliclick/) is somewhat limited (well, more limited than the version I have on my machine :-) ), but it can come in handy. You just have to use an AppleScript do shell script command to use it:
activate application "Final Cut Pro Trial"
delay 0.5
do shell script "/your/path/to/cliclick kd:shift kp:f1 ku:shift"
The delay makes sure everything settles before the click (w/o the delay, click may fail).
(Obviously, replace the path in the example with a correct POSIX path -- mine is installed in /usr/local/bin/)

Related

xcode: application runs properly from Debug folder, but not if copied to any other folder

I have a weirdness with my applescript application created in xcode.
When building for run, it runs properly. When copying the app from the Debug folder to another place, it does not run properly.
The application starts Photoshop, and then uses System Events UI scripting to open the Open File dialog. When not running from the Debug folder, it gets to start Photoshop, also activates System events, it seems to find the Photoshop process, but does not start with the UI scripting.
Being a noob, it baffles me, and I am stuck.
If further information is needed, please bear with me, and let me know what is needed.
Additional information: Another very similar application, which in particular uses exactly the same code, does work without issues.
Also, as asked in a comment, the application is given control to the machine in the Accessibility settings.
Edit: code as requested in comment
set myps to "com.adobe.Photoshop"
set applName to (get name of application id myps)
delay 1
tell application id myps
-- activate
using terms from application "Adobe Photoshop CC 2018"
activate
tell application "System Events"
set ProcessList to name of every process
if (applName is in ProcessList) then
tell process applName
delay 0.8
display alert "here I am"
click menu item 2 of menu 1 of menu bar item 3 of menu bar 1
delay 0.8
tell window 1
-- and so on
"click menu item 2 of menu 1 of menu bar item 3 of menu bar 1" opens the Open File dialog.
When running automatically after building or form the Debug (or Build) folder, that dialog opens; when running from elsewhere, the last thing I get is the "I am here" alert.
Also note that there is another block of code omitted, which uses a plist file to pass some parameters at runtime, but these parameters are needed only later in the script.
Thanks in advance
This is most likely because you need to build for release instead of debug. To do this go to Product -> Scheme -> Edit Scheme. Then click the Run tab on the side and change the Build Configuration to Release.
This will build an executable that should be completely self contained.

Xcode, default window layouts?

No-one has been able to answer this question.
It would seem to be impossible to have XCode open this way.
However Youssef provided the most useful answer - so Youssef gets the points, thank you Youssef.
Note for future readers .. the mac utility Moom is excellent for some, but not all, problems of this nature. Again it is not a total solution.
Using the current up-to-date XCode,
Whenever I OPEN a project in XCode (or start a new project),
it looks like this:
However, I want it to look like this:
Again that's when I OPEN a project.
Is there any way to achieve this? Thanks.
Go to Xcode (in the top menu bar) --> Preferences...
Switch to the Behaviors tab and modify what you want in the Running section
You'll get something like this:
You can choose to show/hide what you need, and specify the default behavior
Cheers
EDIT:
After understanding what you need, I will modify my answer.
There is no way to force XCode to automatically enter a behavior at startup.
The best you can do is to create a custom behavior and give it a keyboard shortcut. And when you start XCode just use the keyboard shortcut to enter the desired behavior.
This is how it is done:
In the behaviors tab (above) click on the + sign at the bottom
Then enter the desired name of this behavior (Maybe StartupBehavior)
Click on the left button to modify the shortcut key. My preferred shortcut key is Command + ` (the button to the left of the '1' key). This is because it will not override any other command and it is easy to click.
Note: you can specify any shortcut you like and dont be afraid to override one that already exists if you don't use it.
Now you need to configure your behavior the way you like it
Close the preference windows.
Now everytime you start XCode just use the shortcut key you specified (Command + ` in my case) to quickly load your behavior
Note:
If you dont like keyboard shortcuts you can also load your behavior by going to Xcode (menu bar) --> Behaviors --> Select your behavior
That is the quickest way you can achieve what you want. I dont think you will be able to force Xcode to run your behavior automatically on startup.
Hope it helped.
Cheers
(not really default layout, you have to hit at least 2 keys simultaneously once XCode is open) You could set up a behavior for "Build->Starts" and hit "CMD + B"
after opening XCode / any Project. This way it will 1. change to your
Layout even if there are any errors and Build fails and 2. you don't
have to Stop it again.
As already said, it's not a default layout, but I like to let the compiler run through projects of other people at the beginning anyway.
You could search for some Keys in XCodes preference file in
~/Library/Preferences/com.apple.Xcode.plist as already suggested in
my comment, I'm not experienced enough to know which are the right
ones, sorry.
Wrap the .xcodeproj File opening in an application. E.g. in no way "clean", but anyway:
Set up an XCode behavior like in option 1
Open Automator, choose Application, drag "open Finder Items" and "run AppleScript" inside.
on run {input, parameters}
set frontmostAppName to name of (info for (path to frontmost application))
repeat while frontmostAppName is not "XCode.app"
set frontmostAppName to name of (info for (path to frontmost application))
delay 0.5
end repeat
try
tell application "System Events" to keystroke "b" using {command down} --simulates CMD + B
end try
return input
end run
Insert some applescript like the above and save the Automator application.
RightClick -> getInfo on any .xcodeproj File in Finder/on Desktop, in "open with" select your new Automator app, click add and back in the info panel -> check "Change All"
You can obviously create a new Behavior instead of using Build->starts, assign a different Hotkey in XCode and script e.g. tell application "System Events" to keystroke "i" using {command down, control down, shift down, alt down} , optimize the applescript somehow or do everything via a real cocoa app if you want
Well, you may be able to approach it using preconfigured xcuserstate files nested in your project templates' default workspace directory, but that is going to be a pain to tweak and maintain. I'm not even sure it would work.
I would personally just approach it by using Behaviors.
So:
1) create a Behavior
2) Give it a convenient key binding
3) Then define the Behavior. For example:
ASCII Behavior Editor:
...
√ Show *Debugger* with *Console View*
√ *Hide* utilities
...
That should do it. Just hit the assigned key command when you want to restore that display state, regardless of its initial state.
Of course, you can do a few other things or all sorts of arbitrary stuff by running an external AppleScript (triggered with a Behavior's run script facility).

Have one application execute an action when another application is given a command using AppleScript?

I know nothing about AppleScript, but I wonder if it could make my life easier: is there a way to write an AppleScript that tells Safari / Firefox / Chrome to refresh the current tab when I save a document in another application, say TextWrangler? Essentially, I want to map the Command+S keyboard shortcut to do two things at once in two separate applications.
If that’s not possible, can you script one application so that saving one file executes a command in another window in that same application?
There are different possible approaches to implement this, but the most straightforward would probably be to create a script that executes all steps you need (i.e. save the document and refresh the window) and bind that to the Cmd+S keyboard combo in the triggering application.
What you need for this approach to work, is, in order:
a method to bind a key combo to a script effective only in a specific application. OS X’ Automator Services fit that bill: their scope can be restricted to a single application (select it in the “only in” drop down at the top of the workflow actions), and they can be assigned a shortcut in the Keyboard preference pane of System Preferences.
a way to relay your commands to the applications they target. AppleScript can help you in two different ways, depending on the fact if your applications are scriptable, i.e. if they have a scripting dictionary you can inspect in the AppleScript editor:
if they do, and their terminology includes the saving action for the editor on one side (most scriptable document based apps do so in the form save <document>), the page refreshing for the browser(s) on the other (Chrome has reload <tab>, Safari gets the same result via a JavaScript detour, i.e. do JavaScript "window.location.reload()" in <document> – I don’t use Firefox), you are set.
if they do not, GUI Scripting might help, i.e. simulating a click on the right UI element (menu or toolbar) via tell application "System Events" to tell process <your process> to click item x of menu y.
That script can then be embedded into the Automator workflow (in an “Run AppleScript” action, to be precise).
As you can see, a lot depends on the exact software you are using. if you are new to AppleScript and the above baffles you, I’d suggest spending a bit of time on the AppleScript pages of Mac OS X Automation (where you’ll also find example scripts which will kick start you into things like GUI Scripting).
One final note: as of this writing, sandboxed applications do not honor key combos assigned to them in the Keyboard Preference pane (they do honor global key combos set there – just not those specifically targeting them). This means you cannot, for instance, currently override TextEdit’s Cmd+S shortcut for saving in Lion. As long as your editor is not sandboxed (easily checked in Activity Monitor), you should have no issue with this.
One solution would be to create a folder action to refresh the current tab when a new file is saved in the folder.
on adding folder items to theFolder after receiving theFiles
tell application "Google Chrome"
activate
tell window 1 to tell active tab
set URL to (get URL)
end tell
end tell
end adding folder items to

"Sort Lines in Selection" for Xcode 4

I like Textmate's "Sort Lines in Selection" feature a lot. Is there something similar for Xcode 4? If not, what would be the best way to integrate such a feature (a plugin, Applescript, ...)?
You can do this with Automator:
Start Automator and select either:"Service" for macOS 10.7 or"New Document" followed by "Quick Action" for macOS 11
Find and drag "Run Shell Script" into the workflow panel
Select "Output replaces selected text"
Type sort -f into the "Run Shell Script" textfield
Save
Now you can sort lines in any textfield. Select some text and right-click or Control click and select the service you just created.
After 4 years Xcode still doesn't have this feature builtin, but now it supports extensions. So here you go: "xcsort" is an extension to
sort text in Xcode 8. It adds a command to sort lines in selection.
For macOS 10.14+ (Mojave) users who are looking at epatel's answer, Automator has renamed Services to Quick Action. I was lost for a moment until I figured it out.
How
In TextMate, open the bundle and see how they have implemented it ;)
Specifically, they have used sort -f for that command.
In Xcode
Xc4 doesn't offer external commands, but Xc3 did... what version are you using?
Here's an overview for Xc3's script system: http://www.mactech.com/articles/mactech/Vol.23/23.01/2301XCode/index.html
Xc4 allows you to run an external script via Behaviours, but you cannot pass or return text/selection.
AppleScript
You may be able to do it with AppleScript... every time I have tried to do anything nontrivial with AS + Xcode, it didn't work out very reliably (if at all). But that was with Xc3 - locating what you need may be easier with Xc4 (unified UI and all).
I just use TextMate for this.
There's a bug on 10.7 and 10.8 where the shortcuts for Automator services don't always work until the services menu has been shown once from the menu bar. You can still select them from the context menu, but another option would be to assign a shortcut to a script like this:
try
set old to the clipboard as record
end try
try
tell application "System Events" to keystroke "c" using command down
do shell script "export LC_CTYPE=UTF-8; pbpaste | sort -fn | pbcopy"
tell application "System Events" to keystroke "v" using command down
delay 0.05
end try
try
set the clipboard to old
end try
Trying to get the clipboard when it's empty results in an error. pbpaste and pbcopy don't support Unicode by default in the environment used by do shell script.

How to use Automator to bring window to front?

I'm using a Mac, OSX 10.6 and I have a function in a desktop application that I want to automate. Manually I press Command+R wait for the application to read some data form a physical device for 1 minute, then press command+R again to take another reading (at this point it asks me if I want to save the data, so I press tab, tab and then space bar to select to save the data. I do this 3 times in total, so I want to automate the 3 times, so I can walk away from the computer and it will read 3 times automatically.
Is automator the best way to do this?
I've tried to do this already in automator by using the 'watch me do' function. This starts with the 'bring window Untitled to the front', and then the second command is press command+R. I then found a little piece of apple script to wait 1 minute and I plug the first action into that for the wait function.
However, when I click run or step, instead of going and opening up the correct window ("Untitled"), the cursor moves to the Media button in automator, and clicks that instead! But the application is definitely listed as the correct one.
Any help appreciated, but maybe automator is the wrong way to go?
Apple Script is the best way to go for things that don't require any "special processing" that would need to be done by a chain of different applications.
1) uging the AppleScript Utility
make sure that you have GUI scripting enabled in the "AppleScript Utility"
2) using the Script Editor choose File>Open Library and see if your application has any scriptable functions ... these may be a better way to go.
3) Create a new script and put in something like this ...
tell application "Firefox"
activate
delay 1 -- give it time to react
repeat 3 times
-- this gives us the keyboard
tell application "System Events"
keystroke "r" using {command down}
end tell
delay 6
end repeat
end tell
I used Firefox to test it .... should work for you ....
Once you've got the script you can use the save as to make it into an app or save it as a script in your ~/Library/Scripts folder or paste it into an automator workflow and schedule it with iCal.
I don't think automator is the way to go. You could use applescript, but you should take a look at sikuli. You will need to write the Sikuli script yourself, but what you describe shouldn't be difficult

Resources