I'm trying to fully automate a workflow for getting images into iPhoto. The last piece of the puzzle is handling duplicate images. Currently I see no way in Applescripts import command to not import duplicates. Therefore when a duplicate arrises all workflow stops and iPhoto waits for input.
Is there a way to get through this?
You will need to script this. Here is a bit of code that seems to work inside applescript editor. Adjust accordingly for automator...
set iFolder to (choose folder)
set iFiles to (list folder iFolder)
tell application "iPhoto"
repeat with iFile in iFiles
try
set pFound to get (every photo of album "Photos" whose image filename is iFile)
end try
if length of pFound is not 0 then
log ("File '" & iFile as text) & "' exists..."
# Move or delete it here
end if
end repeat
# Continue with import
import from (iFolder as alias)
end tell
Related
My goal is to create a script that will allow me to paste images into a social media platform's new post instance. This platform allows pasting images from Clipboard.
My logic is:
Open the File Select modal with a specific default path.
Select a file by user.
Pass the selected object to the Clipboard.
Send the Paste command back into the Post instance.
I was able to complete all of this; the only issue I'm having is setting the absolute starting path of the File Select modal; no matter what I do, or how I pass the absolute path, it seems to always open the path of the last physical action outside the script, in other words, if I physically opened a Explorer from Windows itself or any other app, it will use that path, instead of the one I mentioned in the script; it's been driving me nuts. I've rebooted the script, refreshed everything, cleared cache, did a rain dance.
Would love some assistance!
Here's my script:
^+x::
mypath := "C:\Users\MyComputerName\Downloads\SOCIAL-MEDIA\Images"
FileSelectFile, SelectedFile, 3, %mypath%
if (SelectedFile = "")
MsgBox, The user didn't select anything.
else
pToken := Gdip_Startup()
Gdip_SetBitmapToClipboard(pBitmap := Gdip_CreateBitmapFromFile(SelectedFile))
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
send ^v
return
It seems that setting the third parameter to "2" aka "Path must exist," did the trick.
https://www.autohotkey.com/docs/v1/lib/FileSelectFile.htm
I have the following AppleScript that works fine when I step through each line one at a time using Script Debugger, but reports that the _doc variable has a missing value when it gets the the save as line.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
set _folder to choose folder
set _files to files of _folder
repeat with _file in _files
if creator type of _file is "MSWD" then
tell application "Microsoft Word"
open _file
set _doc to document of window 1
save as _doc file format format text
close _doc
end tell
end if
end repeat
end tell
I've tried pausing for as long as 5 seconds using delay 5 with no change in the behavior. Why might this be happening and what can I do about it?
The answer to "why might this be happening" seems to be "it probably is a timing problem" and "because there are a number of problems when automating Word from both AppleScript and VBA on Mac that Microsoft has not yet fixed". I don't think there's much you can do about it except report to Microsoft via the Smiley mechanism or via word.uservoice.com. On uservoice, best to add your vote than existing request if there is one. But there is no reason at all to believe that Microsoft will even acknowledge or fix quite serious automation problems at the moment.
I had not come across the problem where you couldn't even set _doc to document of window 1 successfully. I was always able to use
set _doc to open _file
Here, I found a "delay 5" was enough to solve the problem you report, but there has also long been a problem where the "_doc" variable becomes invalid after a save as. I had a solution to that that iterates through the windows, so have put together this script which
a. should reduce the delay as much as possible
b. works on simple test data here but could do with improvement, especially on error checking
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
set _folder to choose folder
set _files to files of _folder
repeat with _file in _files
if creator type of _file is "MSWD" then
tell application "Microsoft Word"
--activate
set doc_count to (count of documents)
open _file
-- You have to set the maximum no. of repeats
-- high enough for your system
set repeats to 50
repeat until (count of documents) > doc_count or repeats = 0
set repeats to repeats - 1
end repeat
if (count of documents) > doc_count then
set _doc to (document (doc_count + 1))
set _windows to the windows
repeat with _window in _windows
if the full name of the document of _window is the full name of _doc then
set _windowIndex to the entry_index of _window
exit repeat
end if
end repeat
-- you need to create a new file name for each file.
-- this is a temporary kludge
set _textfilename to (posix full name of _doc) & ".txt"
save as _doc file name _textfilename file format format text
-- _doc now invalid, we need to "reconnect"
set _windows to the windows
repeat with _window in _windows
if the entry_index of _window is _windowIndex then
set _doc to the document of _window
exit repeat
end if
end repeat
close _doc saving no
else
-- you can make this more informative, and you might still need to
-- try to close something.
display dialog "Could not open document: " & POSIX path of _file
end if
end tell
end if
end repeat
end tell
Incidentally, when I tested on a single document using open recent file instead, there was never any problem getting a reference to the document. But that's useless for the kind of thing you're trying to do.
I'm working on an App in app designer. Within the app the user will select a bunch of options before running some calculations.
To simplify this process I added a "Save as..." menu so that the user can save the current settings to a file (.mat) and reload them when they open the app the next time.
What I'm trying to achieve is that the user can double click on the previously saved .mat file, which will launch the app and the app will automatically read the double clicked file and load all the settings.
All this needs to happen after the app is compiled and distributed as an executable.
I'm thinking that one way to achieve this is to make a startup window of the app that calls the main window passing the file path as parameter.
Any suggestion would be really appreciated.
Hi, I think I may have a fairly simple, albeit involved, solution for you.
Brief solution overview (TL;DR)
Save the settings from the app with an extension other than .mat, e.g. .mydat. Add an App Input Argument and have the startupFcn treat the argument as a file name to a *.mydat file and be sure to also handle the case that the argument is left out. After the first output file is saved, use windows Open with... to select your app. Now double clicking the *.mydat file will open your app's .exe and will provide the file name of the clicked file to the input argument in your startupFcn.
An example in MATLAB 2018a as a compiled exe on windows 10.
Ok, to start. Let's setup a simple app (I called it runAppFromData) that takes a string input to an edit field and saves it in a file called 'settingsValues.mydat'. It looks like:
The callback for the Save button collects the Value into a local variable called value and then saves it to disk:
% Button pushed function: Save
function save(app, event)
value = app.InputField.Value;%#ok
% User selects save location
saveLocation = uigetdir();
% Now just save the value variable to the selected location.
save(fullfile(saveLocation,'settingsValues.mydat'), 'value', '-mat');
end
I don't know when appdesigner added the feature to "run app with inputs" but I have it with 2018a:
We make a single input, fileName that expects a file name as a string (you'll see why below). So add the input and click OK. Then we're sent to "code view" at the startupFcn. Here we'll write the logic that parses the input file. For my simple example app, I load the input file into a struct and then send the value to the edit field:
% Code that executes after component creation
function startupFcn(app, fileName)
if nargin < 2 % app is itself an argument
% just continue running the application without error
return
end
% fileName is a string, so let's load it into a struc
S = load(fileName, '-mat');
% The value field will be there because that is how we wrote it
app.InputField.Value = S.value;
end
Note, I performed a nargin check to handle the first-run case (and anytime the app is run from the actual executable).
MATLAB doesn't care what the file extension is of a matlab file and if you have an unknown file extension, e.g. .mydata, double-clicking the file in windows will ask you to choose the application, which works to your benefit for deployment:
A couple things to consider.
When the app is opened from the .exe it will always show the default values. If you want to input some other default values you can edit your windows shortcut Target field to supply a file path for the desired input file (see here). This saves recompiling with new defaults, but the file has to remain somewhere (you can package it with the app too).
Sorry this answer got soo long! I hope it helps!
You can't double click a .mat file and open an entire executable, but you can definitely add a startup function that asks you to open a .mat file. My suggestion though would be to make sure that you have a template file at least in place, so that the user doesn't run into problems the first time running the program where there is no file to open.
I'm trying to set it so that there's no user interaction when I open up my illustrator file using applescript, but the standard:
tell application id "com.adobe.Illustrator"
activate
set user interaction level to never interact
open theFile without dialogs
doesn't work for this plugin I have installed that checks for white overprints.
If it were up to me I'd just uninstall the plugin but it's for a work pc.
I also tried clicking the button automatically (with help from Tim Joe) by using:
try
tell application "System Events"
tell process "Finder"
click button "OK" of window "Adobe Illustrator"
end tell
end tell
end try
and I've tried
tell application "System Events"
tell process "Adobe Illustrator"
keystroke return
end tell
end tell
Does anyone know a way of solving this?
below is the full code as it currently stands:
set saveLocation to ((path to desktop) as string) --place to save the files
set theFile to choose file with prompt "Choose the Illustrator file to get outlines on"
set outputFolder to choose folder with prompt "Select the output folder"
tell application "Finder" to set fileName to name of theFile
set fullPath to (saveLocation & fileName) --file path of new .ai
set fileName to (text 1 thru ((length of fileName) - 3) of fileName) --remove .ai from fileName
set olPath to text 1 thru ((length of fullPath) - 3) of fullPath & "_OL.ai" --path of outlined file
tell application id "com.adobe.Illustrator"
activate
ignoring application responses
open theFile without dialogs
end ignoring
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
save current document in file fullPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save file to desktop
convert to paths (every text frame of current document) --convert text to paths
save current document in file olPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save another copy to desktop with name + _OL.ai
end tell
tell application "Finder"
set newFolder to make new folder at saveLocation with properties {name:fileName}
move fullPath to newFolder --create new folder and move both new files into it
move olPath to newFolder
set newFolderPath to (newFolder) as string
set newFolderPath to text 1 thru -2 of newFolderPath --remove the trailing ":"
tell current application --zip up the new folder
set qpp to quoted form of POSIX path of newFolderPath
do shell script "cd $(dirname " & qpp & ")
zip -r \"$(basename " & qpp & ").zip\" \"$(basename " & qpp & ")\""
end tell
set zipFile to newFolderPath & ".zip"
move zipFile to outputFolder --move .zip to output
delete newFolder --delete folder on desktop left from zipping
end tell
--prepare a notification email
set presetText to "Hello,
Files Uploaded:
" & fileName & ".zip
To access our FTP Server:
http://217.207.130.162:8080/WebInterface/login.html
To access our FTP server, log onto our website below:
Username:
Password:
Thanks,
Joe"
tell application "Mail" --open up prepared email
activate
set theMEssage to make new outgoing message with properties {visible:true, subject:fileName, content:presetText}
end tell
--open file containing usernames and passwords for the FTP
do shell script "open /Users/produser/Desktop/FTP_Users"
I tracked down and installed White Overprint Detector I could see what you mean. I had to use an older version as I only have CS3, and I saw the dialog it produces when you open a document. The following worked for me to get it to dismiss:
tell application "Adobe Illustrator" to activate
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
Since my original post seemed too objective to understand I will revise.
With in the tell block for illustrator look for your line that opens the file. Some commands allow with and without properties. Try applying the "without dialogs" property to look something like this.
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
Update:
Two work arounds I can think of. 1) Try telling system events to tell AI to open without dialogs
tell application "system events"
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
end tell
Other is just add in a bit that will just okay the prompt.
try
tell application "System Events"
tell process "Finder"
click button "Continue" of window "Adobe Illustrator"
end tell
end tell
end try
Can try just having it accept the default button.
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
I have a simple Applescript that takes a number of images in a folder that you select and then mirror images the images using Graphic Converter. This script will run if I place it in a new AS file; however, if I try to run it a second time I get the following error "Can't get window 1 of application "GraphicConverter". Invalid index."
This script always ran on OSX 10.6
I'm running OSX 10.7.4 and Graphic Converter 8.1 (latest version).
Here is the script
tell application "Finder"
activate
set loopFinish1 to 0
set pathName to (choose folder with prompt "Choose Folder Containing Images")
set fileList1 to every file of folder pathName
set loopFinish1 to count of items of fileList1
end tell
tell application "GraphicConverter"
activate
repeat with i from 1 to loopFinish1
set currentFile to item i of fileList1
open currentFile as alias
mirror window 1 in horizontal
close window 1 saving yes
end repeat
end tell
This is driving me crazy!
GraphicConverter has other windows (visible and invisible), it's preferable to use the document to get the right window.
Also, perhaps the image doesn't open, so no window, use a try block.
activate
set pathName to (choose folder with prompt "Choose Folder Containing Images")
tell application "Finder" to set fileList1 to (document files of folder pathName) as alias list
tell application "GraphicConverter"
activate
repeat with tFile in fileList1
set currentDoc to open tFile
set CurrWindow to (first window whose its document is currentDoc)
mirror CurrWindow in horizontal
close currentDoc saving yes
end repeat
end tell