I've been trying for a little while to swap out one of the parts in a POSIX Path with a variable but I've had no success. This is the script I'm trying to develop:
tell application "Finder"
duplicate POSIX file "/Users/**Variable**/Documents/img.jpg" to POSIX file "/Users/**Variable**/Desktop/" with replacing
end tell
The parts in the path are the places where I want the variable to be present. How can I do this?
You break the string with ampersands & and the variable.
Also, it's good practice to convert posix paths to Mac standard colon: paths prior to scripting Finder. Finder's duplicate function has been buggy since 10.9 in these ways.
I have broken out some of the coercions so you can see what's going on.
-- define file and user names
set fileName to "img.jpg"
set user1 to "Mitc"
set user2 to "Mitc"
-- create posix path strings
set path1 to "/Users/" & user1 & "/Documents/" & fileName
set path2 to "/Users/" & user2 & "/Desktop/"
-- convert posix paths into colon paths
set ref1 to (POSIX file path1) as string
set ref2 to (POSIX file path2) as string
-- display dialog ref1 & return & ref2 -- to test
tell application "Finder" to duplicate alias ref1 to alias ref2 with replacing
you have to use the " & to tell the script to stop reading the directory and add the variable so here is the result:
tell application "Finder"
duplicate POSIX file "/Users/" & variable & "/Documents/img.jpg" to POSIX file "/Users/" & variable & "/Desktop/" with replacing
end tell
(make sure you use the "set" command to make the variable, EG.)
set variable to "text"
Hope this helped!
Related
This automator script is an example. When i display the path it looks correct, but if i then try to copy files to that path I get an error.
this is the fix - needed posix paths.
on run {input, parameters}
set theFilePath to POSIX path of (input as string) & "/Samples" as string
display dialog theFilePath
return theFilePath
end run
In my Applescript, I'm choosing a file of which I would like to store the path (to be opened later).
When I tried storing the path of the file as a string, this is the error I received:
error "Can’t make path of alias \"Macintosh
HD:Users:Username:Desktop:Folder:File.xls\" into type string." number -1700
from path of alias "Macintosh HD:Users:Username:Desktop:Folder:File.xls" to string
How can I effectively store this file path, so that I may recall it later when opening this file?
Applescript:
tell application "Finder"
set filePath to path of (choose file) as string
set fileName to name of file filePath
end tell
*Note: I also tried as text.
This seems to work:
tell application "Finder"
set filePathAlias to (choose file)
set fileName to name of filePathAlias
set filePath to filePathAlias as string
end tell
I am going to create a self extracting archive but I have got a problem connecting with the default path of the extraction. I would like to extract my files in the same path as the self-extraction archive program. Unfortunately, the files are extracting in another path (C:\Users\computer\AppData\Temp\IXP000.TMP). Is it possible to set the path?
I can't find any direct way to do this with IExpress, but there is a trick we can apply.
But first I'll point out that this is really easy with something like 7-Zip's 7zCon.sfx module (if all you need to do is have the archive extract to the current directory, no questions asked). So you might just want to try something other than IExpress.
Anyhow, the problem with IExpress is that, at the time our install program runs, we're no longer in the directory of the original archive; the current directory is now something like %temp%\IXP000.TMP. So we need to find the directory of our parent process – kind of a pain. Once that's known, we can just xcopy the contents of the archive over to the destination folder.
In VBScript, it would look something like this:
Option Explicit
Dim objShell, objWMI
Dim objCmd, intMyPid, intMyParentPid, objMyParent
Set objShell = CreateObject("WScript.Shell")
Set objWMI = GetObject("winmgmts:root\cimv2")
Set objCmd = objShell.Exec("cmd.exe")
intMyPid = objWMI.Get("Win32_Process.Handle='" & objCmd.ProcessID & "'").ParentProcessId
objCmd.Terminate
intMyParentPid = objWMI.Get("Win32_Process.Handle='" & intMyPid & "'").ParentProcessId
Set objMyParent = objWMI.Get("Win32_Process.Handle='" & intMyParentPid & "'")
objShell.Run "xcopy /y * " & """" & Left(objMyParent.ExecutablePath, _
InStrRev(objMyParent.ExecutablePath, ".exe", -1, vbTextCompare) -1) &_
"\""", 0, True
Your install program would then be, eg: wscript extractToOriginalLocation.vbs //B.
(Inspired somewhat by the answer to this question.)
You could always use a cmd script and echo lines of code into files in specific directories
This script has been working on 10.7 and older, but in 10.8, it seems it's broken. The line:
set theFilePath to ((path to application support from user domain) as rich text) & "AppFolderName:" & UniqueName as string
set theFileReference to open for access theFilePath with write permission
Worked fine on previous versions, but Apple apparently is preventing it from working properly on Mountain Lion. Is there any other way of getting access to that folder via Apple script in Mountain Lion?
Edit: I've included the entire code of the script that will, within a Mail rule export the entire message to a text file that my program can import. The text file is sent to ~/Library/Application Support/MyProgram/MailImport/
Make sure the directory already exists on your machine, as it does here on mine, and the Apple Script doesn't do any checking for it.
This script does not work when path to application support is in the code, but changing it to path to desktop work fine, meaning there is an issue writing to the application support folder, but the code works.
To test, you can create a new rule in Mail, and have Every Message run the script. You have to put the script in ~/Library/Application Scripts/com.apple.mail/
It will then appear as an option in the rules window. You can right-click a message and select Apply Rules to test the script on an individual message.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set sub to subject of eachMessage
set mid to message id of eachMessage
set sen to sender of eachMessage
set recp to ""
repeat with thisRecpt in recipients of eachMessage
set recp to recp & address of thisRecpt & ","
end repeat
set {year:y, month:m, day:d, hours:hh, minutes:mm} to (date sent of eachMessage)
set dat to (y * 10000 + m * 100 + d) as string
set tim to (hh * 100 + mm) as string
set con to content of eachMessage
set TotalString to "<!STDMessageSubject>" & sub & "<!STDMessageSubject>" & "<!STDMessageID>" & mid & "<!STDMessageID>" & "<!STDMessageSender>" & sen & "<!STDMessageSender>" & "<!STDMessageRecipient>" & recp & "<!STDMessageRecipient>" & "<!STDMessageDate>" & dat & "<!STDMessageDate>" & "<!STDMessageTime>" & tim & "<!STDMessageTime>" & "<!STDMessageContent>" & con & "<!STDMessageContent>"
set UniqueName to do shell script "uuidgen"
set theFilePath to ((path to application support from user domain) as rich text) & "MyApplication:MailImport:" & UniqueName as string
set theFileReference to open for access theFilePath with write permission
write TotalString to theFileReference
close access theFileReference
end repeat
end tell
end perform mail action with messages
end using terms from
There's no such thing in applescript as "rich text". It should only be as "text". In addition theFilePath is a string, so in the next line you need to reference it like this... open for access file theFilePath. Notice the word "file". You need that word there to turn the string into a file reference which is what that command requires.
EDIT: Now that I see your entire code I would write it like this. Your problem still may be a sandboxing issue but at the very least you should eliminate any sources of possible coding errors in your script. This will give you the best chance of having a successful script. If it still doesn't work then it probably is a sandboxing issue.
The basic coding issues that I see are that you are telling Mail to perform all of the commands. Mail doesn't know commands such as "path to application support", "do shell script", or how to write to a file. They're applescript commands so you shouldn't tell Mail to perform them. They are not in Mail's applescript dictionary and thus they may be getting confused when Mail tries to perform them. That's certainly the reason that "text" keeps changing to "rich text" as you mention.
So give this a try. If you still have the problem then at least you know that you've done all you can to eliminate sources of errors in your code.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set sub to subject of eachMessage
set mid to message id of eachMessage
set sen to sender of eachMessage
set recp to ""
repeat with thisRecpt in recipients of eachMessage
set recp to recp & address of thisRecpt & ","
end repeat
set {year:y, month:m, day:d, hours:hh, minutes:mm} to (date sent of eachMessage)
set dat to (y * 10000 + m * 100 + d) as string
set tim to (hh * 100 + mm) as string
set con to content of eachMessage
set TotalString to "<!STDMessageSubject>" & sub & "<!STDMessageSubject>" & "<!STDMessageID>" & mid & "<!STDMessageID>" & "<!STDMessageSender>" & sen & "<!STDMessageSender>" & "<!STDMessageRecipient>" & recp & "<!STDMessageRecipient>" & "<!STDMessageDate>" & dat & "<!STDMessageDate>" & "<!STDMessageTime>" & tim & "<!STDMessageTime>" & "<!STDMessageContent>" & con & "<!STDMessageContent>"
my writeToFile(TotalString)
end repeat
end tell
end perform mail action with messages
end using terms from
on writeToFile(TotalString)
set UniqueName to do shell script "uuidgen"
set theFilePath to ((path to application support from user domain) as text) & "MyApplication:MailImport:" & UniqueName
set theFileReference to open for access file theFilePath with write permission
write TotalString to theFileReference
close access theFileReference
end writeToFile
EDIT2: try this handler in place of the one in the above code. This may be one way to make the writeToFile handler work because the writing part would happen in a separate process from the applescript. It's worth a try!
on writeToFile(TotalString)
set UniqueName to do shell script "uuidgen"
set theFilePath to ((path to application support from user domain) as text) & "MyApplication:MailImport:" & UniqueName
set theResult to do shell script "echo " & quoted form of TotalString & " > " & quoted form of POSIX path of theFilePath
end writeToFile
EDIT3: if edit2 doesn't work then look here. It seems others have had problems with Mail writing to certain locations and solved it by adding a key to Mail to give it permission.
So it turns out to be a Sandboxing issue. Apple Mail in 10.8 uses a Sandboxed Application Support folder location in general regardless of how hard you try to just get ~/Library/Application Support/, so from an AppleScript within Mail on 10.8
path to application support from user domain
Returns the path
~/Library/Containers/com.apple.mail/Data/Library/Application Support/
From there the MyApplication:MailImport: folders can be created and accessed. Since our actual program that's trying to read the output isn't sandboxed we can just read and access the data from that location for now, as it seems to be working fine.
I have the following values in a DOS batch file (for example...):
..\Apple\Jones
..\Banana\Smith
..\Pear\Wilson
I need to extract the last name values ("Jones", "Smith", "Wilson") from each value. What one technique can I use that will always give me these substring values?
According to this topic : What is the best way to do a substring in a batch file?
I suggest you to use
%~n0
I already wrote a function for that. You give it any path and it returns you only it's filename or pathname. Works for any path: Url, Windows path, Unix path, etc...
Copy this function at the end of your batch script: (Instructions below)
rem ===========================================================================
:Name_From_Path
SetLocal
set _TMP_FOLDERNAME=%1
for %%g in ("%_TMP_FOLDERNAME%") do set _TMP_FOLDERNAME=%%~nxg
EndLocal & set _Name_From_Path=%_TMP_FOLDERNAME%
goto :EOF
rem ===========================================================================
Usage:
CALL :Name_Of_Path ..\Apple\Jones
ECHO %_Name_From_Path%
Result: Jones