Automator: duplicate OR copy in place - copy

I am trying to build the following services:
Change type of image, result in the same folder (image.jpg => image.jpg + image.png)
Change size of image, result in the same folder (image.jpg => image.jpg + image-800x600.jpg)
I am stuck on part where the original image is duplicated in the same folder, under a different name (the copy finder item workflow requires a hard coded destination or other option I am not familiar with).
Maybe I could use a shell script to perform the duplicating part. I know how to get the file paths passed to the run shell script workflow, but I can't figure out how to send valid paths out to the next task (change type or resize).
MAC OS version is Mountain lion 10.8.2.

You can duplicate the files before you scale them:
on run {input}
set newFiles to {}
repeat with aFile in input
tell application "Finder" to set myFile to duplicate aFile
set end of newFiles to myFile as alias
end repeat
delay 1
return newFiles
end run
You can add another AppleScript at the end to deal with the files names:
on run {input}
repeat with myFile in input
tell application "System Events" to set oldName to myFile's name
set newName to do shell script "echo " & quoted form of oldName & " | sed -E 's/ ?copy ?[0-9?]*//'"
tell application "System Events" to set myFile's name to newName
end repeat
end run

Related

how make link on desktop or in dock with a service just press a right button all that in an applescript executed in automator

The following script is to be placed in a creation of service in automator.
Thus open Automator and select new service. For files and folder link they are placed on the desktop just application can be placed in the Dock ( you have the script below you can modifie all that you want.
On the left part of automator select utility, double click on Execute an script of applescript. In a right part you see window open, on the item " service receive a select " you choose " Files and Folders ".
Now below in window "Execute an Applescript " inside
" on run {input,parameters}"
you can put the script below.
on run {input, parameters}
set input to POSIX path of input
set fich to POSIX path of input
set ft to {}
set fold to {}
set sn to {}
set Nm to {}
set {file type:ft} to info for POSIX path of fich
if ft is not "APPL" then
set input to input
set vhdoudmg to input as Unicode text
set input to POSIX path of vhdoudmg
set sn to ""
try
set {name:Nm} to info for file vhdoudmg
end try
if sn is "" then
set lelien to "/Users/username/Desktop/
" & Nm
else
set lelien to "/Users/username/Desktop/
" & sn
end if
set vhdoudmg to do shell script "ln -s -v " & quoted form of input & " " & quoted form of lelien
else
set Reponse to display dialog " Choose to create the symbolic link on the Desktop or Application in the Dock " & return buttons {" The link on the Desktop ", " Application in the Dock "}
set Choix to button returned of Reponse
if " Application in the Dock " is in Choix then
try
set theFile to POSIX path of input
end try
try
tell application "Dock" to quit
end try
do shell script "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</k ey><string>" & theFile & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>' "
try
tell application "Dock" to activate
end try
else
set input to POSIX path of input
set vhdoudmg to input as Unicode text
set input to POSIX path of vhdoudmg
set fich to do shell script "echo " & input
try
set {name:Nm} to info for file vhdoudmg
end try
set lelien to "/Users/username/Desktop/" & Nm
set vhdoudmg to do shell script "ln -s -v " & quoted form of input & " " & quoted form of lelien
end if
end if
return input
end run
And now you can place this workflow in /Users/yourname/Library/Services and when you click rigth on your mouse button, il you go on context menu on Services in a choice you can see the name of this script ( the name which you would have given it during the creation.)
Thus open Automator and select new service. For files and folder link they are placed on the desktop just application can be placed in the Dock ( you have the script below you can modifie all that you want.
Good evening just for a shorter and just as effective script.
on run {input, parameters}
set input to POSIX path of input
set fich to POSIX path of input
set ft to {}
set Nm to {}
set {name:Nm,file type:ft} to info for POSIX path of fich
set input to input
set vhdoudmg to input as Unicode text
set input to POSIX path of vhdoudmg
set lelien to "/Users/username/Desktop/" & Nm
if ft is not "APPL" then
set vhdoudmg to do shell script "ln -s -v " & quoted form of input & " " & quoted form of lelien
else
set Reponse to display dialog " Choose to create the symbolic link on the Desktop or Application in the Dock " & return buttons {" The link on the Desktop ", " Application in the Dock "}
set Choix to button returned of Reponse
if " Application in the Dock " is in Choix then
try
set theFile to POSIX path of input
end try
try
tell application "Dock" to quit
end try
do shell script "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & theFile & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
try
tell application "Dock" to activate
end try
else
set vhdoudmg to do shell script "ln -s -v " & quoted form of input & " " & quoted form of lelien
end if
end if
return input
end run

How do you in Applescript cp a file to a folder with a variable in name and or prompt for credentials without shell script?

I have an issue where I need it to prompt for admin credentials if needed and cant find a workable solution.
set foo to computer name of (system info)
set p to (path to desktop)
set targetFile to "Macintosh HD:private:var:log:system.log:"
set targetPath to p & "LOGS-I-NEED-" & foo as text
try
if ("80" is not in (do shell script "id -G")) then
tell application "Finder" to duplicate file targetFile to targetPath
else
tell application "Finder" to duplicate file targetFile to targetPath
end if
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message & return & "targetFile:" & targetFile & return & return & "targetPath:" & targetPath buttons {"OK"} default button 1
end try
I have also tried this but dont know how to cp to a folder with a variable in its name.
try
set foo to computer name of (system info)
do shell script "sudo cp /Private/var/log/system.log ~/Desktop/{name:LOGS-I-NEED} & foo" with administrator privileges
end try
Either way will work for me.
Thanks Guys!
quoted form of surrounds strings with single quotes and replaces ' with '\''.
do shell script "d=~/Desktop/LOGS-I-NEED-" & quoted form of (computer name of (system info)) & "
mkdir -p \"$d\"
cp /var/log/system.log \"$d\"" with administrator privileges
You can also get the computer name with systemsetup:
do shell script "d=~/\"Desktop/LOGS-I-NEED-$(systemsetup -getcomputername | sed 's/[^:]*: //')\"
mkdir -p \"$d\"
cp /var/log/system.log \"$d\"" with administrator privileges
If the duplicate command needs root privileges, Finder should show a password dialog. But you could also run a script like this with something like sudo osascript Untitled.scpt:
set d to "LOGS-I-NEED-" & computer name of (system info) as text
tell application "Finder"
if not (exists folder d of desktop) then
make new folder at desktop with properties {name:d}
end if
duplicate POSIX file "/var/log/system.log" to folder d of desktop
end tell

Open file by name only, no extension

How can I open any type of file in a .bat by providing only a name of the file, no extension?
I want to let windows decide the application to use.
example:
%SystemRoot%\explorer.exe E:\SomeFolder\
%SystemRoot%\explorer.exe E:\SomeFolder\file1
Use START command:
start "Any title" E:\SomeFolder\
start "Any title" E:\SomeFolder\file1
Taken from Start help:
If Command Extensions are enabled, external command invocation
through the command line or the START command changes as follows:
.
non-executable files may be invoked through their file association just
by typing the name of the file as a command. (e.g. WORD.DOC would
launch the application associated with the .DOC file extension).
See the ASSOC and FTYPE commands for how to create these
associations from within a command script.
.
When searching for an executable, if there is no match on any extension,
then looks to see if the name matches a directory name. If it does, the
START command launches the Explorer on that path. If done from the
command line, it is the equivalent to doing a CD /D to that path.
Note that previous description imply that the pure filename must also execute the right application, with no START command. To pick up the first file with a given name:
for %%f in (name.*) do set "filename=%%f" & goto continue
:continue
... and to execute it:
%filename%
PS - Note that you want "to let windows decide the application to use", but in your example you explicitly select %SystemRoot%\explorer.exe as the application to use. So?

I want to prompt a user for the first 5 characters of a file then have it search for the files

I'm trying to write a script that will prompt the user for the first 5 charters of a file name then have it search the directories for any files that start with that. Then I want it to check to see if a folder is created with the file names and if not create one then move the files there. But if there is a directory for it already then to just move the files too the folder.
Break it down step by step:
"prompt the user for the first 5 characters of a file name" -- you can use the shell read command to get the data. Try a simple shell script:
#!/bin/bash
read foo
echo "foo = $foo"
"if a folder is created with the file names" -- you can use find to see if a file exists. for example:
find . -name abcde\*
"But if there is a directory for it already then to just move the files too the folder." -- the command mkdir takes a -p option so that, if the directory already exists, it won't do anything.

Open text file and program shortcut in a Windows batch file

I have two files in the same folder that I'd like to run. One is a .txt file, and the other is the program shortcut to an .exe. I'd like to make a batch file in the same location to open the text file and the shortcut then close the batch file (but the text file and program remain open).
I tried this with no luck:
open "myfile.txt"
open "myshortcut.lnk"
Also didn't work:
start "myfile.txt"
start "myshortcut.lnk"
I was able to figure out the solution:
start notepad "myfile.txt"
"myshortcut.lnk"
exit
This would have worked too. The first quoted pair are interpreted as a window title name in the start command.
start "" "myfile.txt"
start "" "myshortcut.lnk"
Don't put quotes around the name of the file that you are trying to open; start "myfile.txt" opens a new command prompt with the title myfile.txt, while start myfile.txt opens myfile.txt in Notepad. There's no easy solution in the case where you want to start a console application with a space in its file name, but for other applications, start "" "my file.txt" works.
The command-line syntax for opening a text file is:
type filename.txt
File types supported by this command include (but are not limited to): .doc, .txt, .html, .log
If the contents is too long, you can add "|more" after "type filename.txt", and it will pause after each screen; to end the command before the end of the file, you can hold Ctrl + C.
I use
#echo off
Start notepad "filename.txt"
exit
to open the file.
Another example is
#echo off
start chrome "filename.html"
pause
You can also do:
start notepad "C:\Users\kemp\INSTALL\Text1.txt"
The C:\Users\kemp\Install\ is your PATH. The Text1.txt is the FILE.
"location of notepad file" > notepad Filename
C:\Users\Desktop\Anaconda> notepad myfile
works for me! :)
In some cases, when opening a LNK file it is expecting the end of the application run.
In such cases it is better to use the following syntax (so you do not have to wait the end of the application):
START /B /I "MyTitleApp" "myshortcut.lnk"
To open a TXT file can be in the way already indicated (because notepad.exxe not interrupt the execution of the start command)
START notepad "myfile.txt"
The command start [filename] opened the file in my default text editor.
This command also worked for opening a non-.txt file.
If you are trying to open an application such as Chrome or Microsoft Word use this:
#echo off
start "__App_Name__" "__App_Path__.exe"
And repeat this for all of the applications you want to open.
P.S.: This will open the applications you select at once so don't insert too many.
Try using:
#ECHO off
ECHO Hello World!
START /MAX D:\SA\pro\hello.txt
Its very simple,
1)Just go on directory where the file us stored
2)then enter command i.e. type filename.file_extention
e.g type MyFile.tx
To open a file with default software just need to type the path of the file or, if you are at the file location, the file name.
C:\Users\MyName>C:\User\MyName\Desktop\hello.txt
or
C:\Users\MyName\Desktop>hello.txt
If you want specific program like notepad you can specify it first.
C:\Users\MyName>notepad C:\User\MyName\Desktop\hello.txt
or
C:\Users\MyName\Desktop>notepad hello.txt
Note that notepad is usually default text editor for .txt, in this case would make more sense to type notebook only to open a .cs/.cpp/.py file if your default for that files is any IDE and you just want to see the file on notebook
Regarding the batch file it will work the same way but to open them at the same time and let the command line go away you should use:
start "title" {filename}
So the command can open the file and return to next line immediately.
start "" C:\Users\MyName\MyFolder\foo.exe
start "" C:\Users\MyName\MyFolder\notes.txt
or
start "" foo.exe
start "" notes.txt
The last one only works if the batch file is on the same location of the files.
If you plan on using the console to open the batch file and you want the console to close at the end you should indeed write exit on last line.
When in doubt, it always helps to read the docs:
>help start
Starts a separate window to run a specified program or command.
START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
[command/program] [parameters]
"title" Title to display in window title bar.
path Starting directory.
B Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application.
I The new environment will be the original environment passed
to the cmd.exe and not the current environment.
MIN Start window minimized.
MAX Start window maximized.
SEPARATE Start 16-bit Windows program in separate memory space.
SHARED Start 16-bit Windows program in shared memory space.
LOW Start application in the IDLE priority class.
NORMAL Start application in the NORMAL priority class.
HIGH Start application in the HIGH priority class.
REALTIME Start application in the REALTIME priority class.
ABOVENORMAL Start application in the ABOVENORMAL priority class.
BELOWNORMAL Start application in the BELOWNORMAL priority class.
NODE Specifies the preferred Non-Uniform Memory Architecture (NUMA)
node as a decimal integer.
AFFINITY Specifies the processor affinity mask as a hexadecimal number.
Picture for the visual learners: