在 applescript 中获取所选文件的名称
Getting the name of a chosen file in applescript
我需要我的 AppleScript 知道用户在 choose file
命令中选择的文件的名称。听起来应该很简单,但我想不出答案。该脚本从 gif 文件中提取帧,并将各个图像放入应用程序内容内的文件夹中。然后它会迅速将桌面背景更改为文件夹内的图像,从而为您提供壁纸的 gif。但是,如果不知道所选 gif 文件的名称,我将无法执行此操作,因为我不知道文件夹中图像的名称。如果有其他一些简单的方法可以解决这个问题,那也很好。这是我目前所拥有的:
on delay duration
set endTime to (current date) + duration
repeat while (current date) is less than endTime
tell AppleScript to delay duration
end repeat
end delay
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
set dest to quoted form of POSIX path of ((path to me as string) & "Contents:Resources:Gif")
set pScript to quoted form of "from AppKit import NSApplication, NSImage, NSImageCurrentFrame, NSGIFFileType; import sys, os
tName=os.path.basename(sys.argv[1])
dir=sys.argv[2]
app=NSApplication.sharedApplication()
img=NSImage.alloc().initWithContentsOfFile_(sys.argv[1])
if img:
gifRep=img.representations()[0]
frames=gifRep.valueForProperty_('NSImageFrameCount')
if frames:
for i in range(frames.intValue()):
gifRep.setProperty_withValue_(NSImageCurrentFrame, i)
gifRep.representationUsingType_properties_(NSGIFFileType, None).writeToFile_atomically_(dir + tName + ' ' + str(i + 1).zfill(2) + '.gif', True)
print (i + 1)"
repeat with f in gifFiles
set numberOfExtractedGIFs to (do shell script "/usr/bin/python -c " & pScript & " " & (quoted form of POSIX path of f) & " " & dest) as integer
end repeat
repeat
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
你可以通过System Events
得到名字
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
tell application "System Events" to set gifFileName to name of gifFiles
或使用 info for
命令,虽然多年来它已被弃用,但它甚至在 El Capitan 中仍然有效
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
set gifFileName to name of (info for gifFiles)
当然可以使用Finder,但建议尽量避免使用Finder
要获取文件名,您必须使用 Finder 名称 属性 :
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
tell application "Finder" to set myName to name of file gifFiles
变量 myName 包含所选文件的名称(带扩展名)。
我需要我的 AppleScript 知道用户在 choose file
命令中选择的文件的名称。听起来应该很简单,但我想不出答案。该脚本从 gif 文件中提取帧,并将各个图像放入应用程序内容内的文件夹中。然后它会迅速将桌面背景更改为文件夹内的图像,从而为您提供壁纸的 gif。但是,如果不知道所选 gif 文件的名称,我将无法执行此操作,因为我不知道文件夹中图像的名称。如果有其他一些简单的方法可以解决这个问题,那也很好。这是我目前所拥有的:
on delay duration
set endTime to (current date) + duration
repeat while (current date) is less than endTime
tell AppleScript to delay duration
end repeat
end delay
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
set dest to quoted form of POSIX path of ((path to me as string) & "Contents:Resources:Gif")
set pScript to quoted form of "from AppKit import NSApplication, NSImage, NSImageCurrentFrame, NSGIFFileType; import sys, os
tName=os.path.basename(sys.argv[1])
dir=sys.argv[2]
app=NSApplication.sharedApplication()
img=NSImage.alloc().initWithContentsOfFile_(sys.argv[1])
if img:
gifRep=img.representations()[0]
frames=gifRep.valueForProperty_('NSImageFrameCount')
if frames:
for i in range(frames.intValue()):
gifRep.setProperty_withValue_(NSImageCurrentFrame, i)
gifRep.representationUsingType_properties_(NSGIFFileType, None).writeToFile_atomically_(dir + tName + ' ' + str(i + 1).zfill(2) + '.gif', True)
print (i + 1)"
repeat with f in gifFiles
set numberOfExtractedGIFs to (do shell script "/usr/bin/python -c " & pScript & " " & (quoted form of POSIX path of f) & " " & dest) as integer
end repeat
repeat
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
你可以通过System Events
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
tell application "System Events" to set gifFileName to name of gifFiles
或使用 info for
命令,虽然多年来它已被弃用,但它甚至在 El Capitan 中仍然有效
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
set gifFileName to name of (info for gifFiles)
当然可以使用Finder,但建议尽量避免使用Finder
要获取文件名,您必须使用 Finder 名称 属性 :
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
tell application "Finder" to set myName to name of file gifFiles
变量 myName 包含所选文件的名称(带扩展名)。