AppleScript returning Null() - applescript

When I run this AppleScript I'm getting the error (null). The only thing I can think of is that the file path is not formatted correctly. Any help would be appreciated.
on run {input, parameters}
set picFolder to alias "Users:cd:Dropbox (Personal):Camera Uploads:"
set screenshotFolder to alias "Users:cd:Dropbox (Personal):Camera Uploads:Screenshots:"
tell application "System Events"
set photos to path of files of picFolder whose kind is "Portable Network Graphics image"
end tell
set screenshots to {}
repeat with imgPath in photos
set imgAlias to alias imgPath
tell application "Image Events"
set img to open imgPath
if ((dimensions of img = {640, 1136}) or (dimensions of img = {640, 960}) or (dimensions of img = {1024, 768})) then
set end of screenshots to imgAlias
end if
close img
end tell
end repeat
tell application "Finder"
move screenshots to screenshotFolder
end tell
return input
end run

The main script works for me, but not if I run it the way it is in my script editor. I have to comment out {input, parameters} and return input, because a run handler (in this context) can't accept parameters (or you can change the name of the handler). If I do that there doesn't seem to be a problem (aside from obvious lack of error correction, e.g. if file already exists, etc). You can use parameters for a run handler, but you can't test it in the script editor that way (I'm assuming you want to send parameters eventually from the command line).

Grant to Image Events Full Disk Access.
and replace
set img to open imgPath
with
set img to open file imgPath
After
tell application "Image Events"
add
launch

Related

AppleScript - relinking file in Indesign - Cannot create the link resource from the given URI

I am trying to make a Folder action in Automator, which duplicates specific .indd file, opens it in InDesign and then relinks "test1.png" with a file that was dropped in the folder, which is bound to the Automator action.
I can get it working with hardcoded file path, but if I try getting the file path dynamically it gives me error "Cannot create the link resource from the given URI"
It's probably something very simple, but I am new to AppleScript and can't figure it out.
Code:
on run {input, parameters}
tell application "Finder"
set filename to name of file input
end tell
tell application "Adobe InDesign 2020"
if document 1 exists then
tell document 1
relink link "test1.png" to "Macintosh HD" & filename
try
update link "test1.png"
end try
end tell
end if
end tell
return input
end run
I don't have Adobe InDesign to be able to test this but try this.
on run {input, parameters}
set theFile to input's item 1
tell application "Adobe InDesign 2020"
if document 1 exists then
tell document 1
relink link "test1.png" to theFile
try
update link "test1.png"
end try
end tell
end if
end tell
end run

MacOS Automator + Applescript solution for exporting docx to pdf

Scratching my head after reading lots of different threads on this and tried a bunch of scripts but none seem to work.
I'd like to use Automator to automate Word 2016 conversion of a selection of docx files to pdf.
Used the following Automator Service:
Used the following script:
on run {input, parameters}
tell application id "com.microsoft.Word"
activate
open input
set doc to name of active window
set theOutputPath to (input & ".pdf")
save as active document file name theOutputPath file format format PDF
end tell
end run
Which results in error: Microsoft Word got an error: active document doesn’t understand the “save as” message.
The main issue is that input is a list. You have to use a repeat loop to process each file separately
I added a line to close the current document after having been converted
on run {input, parameters}
tell application id "com.microsoft.Word"
activate
repeat with aFile in input
open aFile
set theOutputPath to ((aFile as text) & ".pdf")
tell active document
save as it file name theOutputPath file format format PDF
close saving no
end tell
end repeat
end tell
end run
To prevent the problem discussed in #vadian's answer, save the file first to Word's default folder (that's usually ~/Library/Containers/com.microsoft.Word/Data/Documents) and then move the file somewhere else.
on run {input, parameters}
repeat with aFile in input
tell application "System Events"
set inputFile to disk item (aFile as text)
set outputFileName to (((name of inputFile) as text) & ".pdf")
end tell
tell application id "com.microsoft.Word"
activate
open aFile
tell active document
save as it file name outputFileName file format format PDF
close saving no
end tell
set defaultPath to get default file path file path type documents path
end tell
tell application "System Events"
set outputPath to (container of inputFile)
set outputFile to disk item outputFileName of folder defaultPath
move outputFile to outputPath
end tell
end repeat
return input
end run

Quicktime Automator + Applescript for QuickTime reference files

I'm trying to create an Automator droplet that allows me to pass one or more video files to an Applescript that then saves out reference files with "_ref.mov" appended to the file name by using QuickTime Player 7.
Saving as a reference is the default save method, which is why I'm not using save reference file function (also, because I can't find any docs on the using descriptors attribute object.)
I found this script on another forum, and it works, but it doesn't accomplish exactly what I need because it requires that 1) a file is open and 2) the user manually specifies an output file name.
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
if not (exists document 1) then display dialog "please open a quicktime movie." buttons {"cancel"} default button 1 with icon 1
set theFile to (choose file name)
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've tried to make changes to accomplish what I need, but my changes break the script:
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
open input
set theFile to ((path of input) & "_ref.mov))
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've also tried:
set theFile to (((path of input) & "_ref.mov") as text) doesn't work
either does as alias or as string
Not sure where I'm going wrong. Please help if you can!

How to save files in same folder applescript

I'm trying to use automator to run an applescript to save illustrator files in their native folder with a new name. Any help with my applescript on this? Have to declare a path.
on run {input, parameters}
tell application "Adobe Illustrator"
set allMyItems to every path item of current document whose fill color is {cyan:0.0, magenta:0.0, yellow:0.0 black:100.0}
repeat with myItem in allMyItems
set fill color of myItem to {cyan:0.0, magenta:0.0, yellow:0.0 black:0.0}
end repeat
set theDestination to path to parentfolder
save current document as eps
close current document
end tell
return input
end run

Relative path to AppleScript exported as .app

I have an AppleScript exported as a .app file because I need it to run on log in.
This is my code:
repeat
tell application "System Events"
repeat with desktopNumber from 1 to count of desktops
tell desktop desktopNumber
set picture to "~/Desktop/script/img.jpg"
end tell
end repeat
end tell
end repeat
The path to the script is ~/Desktop/script/script.app/Contents/Resources/Scripts/main.scpt
I'd like to put the image in the Resources folder as well and make the path relative so i can put the folder anywhere without changing my script so I tried
set desktopPicture to ((container of container of (path to me)) as text) & "/img.jpg"
repeat
tell application "System Events"
repeat with desktopNumber from 1 to count of desktops
tell desktop desktopNumber
set picture to desktopPicture
end tell
end repeat
end tell
end repeat
But that gives me the error Can’t make container of container of alias \"Macintosh HD:Users:Me:Desktop:script:script.app:Contents:Resources:Scripts:main.scpt\" into type text.
System Events is not able to expand the tilde ~.
If you want to refer to ~/Desktop/script/img.jpg using relative paths you could use
tell application "System Events" to set desktopPicture to file "img.jpg" of folder "script" of desktop folder
or
set desktopPicture to alias ((path to desktop folder as text) & "script:img.jpg")
Consider that in both cases AppleScript will throw an error if the file does not exist.

Resources